File Exchange

image thumbnail


version (754 Bytes) by Christopher Harris
Fast "ismember" routine for sorted arrays


Updated 25 Jan 2015

View License

I found that "ismember" is annoyingly slow. I've avoided using it as much as I could, but there are some instances where is it unfortunately necessary. When possible, I try to use "ismembc" or "builtin("_ismemberoneoutput",A,B)" as much as possible (I highly recommend others do the same!), but these are less than optimal as well.
Recently, I found myself in the position of needing to use ismember with two sorted arrays as inputs. After profiling the code, I found that this little function was unforgivably slow. There is no reason there should be so much overhead for two arrays which are pre-sorted.

Anyway, I spent an afternoon writing this silly function. It takes two sorted arrays and returns one array (the same size as the first input).

Example :
>>ismember_mex( [1 3 5], [1 2 3 4 6 7 8] )
ans =


It is quite a bit faster than the normal ismember (or its substitutes) .... (the following was ran in a fresh matlab to avoid that pesky p-code auto-optimizing junk)

>> n = 50000000;
A = sort(ceil(n*rand(n,1)));
B = 1:n;


Elapsed time is 1.042281 seconds.
Elapsed time is 8.390757 seconds.
Elapsed time is 8.598730 seconds.
Elapsed time is 8.298966 seconds.

Finally, I would like to apologize for my code. I like to minimize the number of keypresses, hence things are often jammed together....enjoy.

Cite As

Christopher Harris (2021). ismember_mex.c (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Imran Kanjoo

it is nice and much faster than the ismember.

Dr. Oscar Gaete

nice, it is indeed ~30 times faster in my laptop.
[C,D]=ismember_mex(A,B) does not work, only C=ismember_mex(A,B). That is a really nice feature of the original ismember.
any chance you could improve this?



This file tested significantly faster than the native ismember.m in Matlab 2017a, so thank you for that; however, when I run it on computing clusters it causes them to crash. Do you have any advice besides "Don't be Dumb"? I've passed each worker more than enough memory. Regular ismember works on the clusters.

Narendra Kulkarni

in the newer version of matlab ismember is actually faster than your implementation

>> tic;fastismember(A,B);toc
Elapsed time is 0.539650 seconds.
Elapsed time is 4.208986 seconds.
Elapsed time is 0.366286 seconds.

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!