File Exchange

image thumbnail

ismember_mex.c

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

3 Downloads

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 :
Example:
>>ismember_mex( [1 3 5], [1 2 3 4 6 7 8] )
ans =

1
1
0

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;

tic;ismember_mex(A,B);toc
tic;ismembc(A,B);toc
tic;ismember(A,B);toc
tic;builtin('_ismemberoneoutput',A,B);toc

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 (2020). ismember_mex.c (https://www.mathworks.com/matlabcentral/fileexchange/49150-ismember_mex-c), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

it is nice and much faster than the ismember.

nice, it is indeed ~30 times faster in my laptop.
However:
[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?

cheers

Kyle

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.

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

>> tic;fastismember(A,B);toc
tic;ismembc(A,B);toc
tic;ismember(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