Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function like "ismember" but more efficient?

For example,A is the number set.b is elements.

I want to test whether the number in b is the element of the set A.

I know the matlab function "ismember" could do this ,but it's not fast enough when I use it one million times.

b=[1,2,9,100];
A=[1,2,3,4,5,6,7,8,9];
tic;for ii=1:1e6,ismember(b,A);end;toc
Elapsed time is 45.714583 seconds.

I want to return [1,1,1,0],because 1,2,9 are in the set A,while 100 is not.

Do you know some functions like ismember or some ways more efficient than "ismember"?

like image 473
lihaitao Avatar asked Jul 18 '13 04:07

lihaitao


1 Answers

You can use the mex version, i.e. ismemberoneoutput. The mex version is much faster.

b=[1,2,9,100];
A=[1,2,3,4,5,6,7,8,9];
tic;for ii=1:1e5,ismember(b,A);end;toc
%Elapsed time is 9.537219 seconds. On my pc

% A must be sorted!!! In this example it is already sorted,
% so no need for this here.
tic;for ii=1:1e5,builtin('_ismemberoneoutput',b,A);end;toc
%Elapsed time is 0.376556 seconds. On my pc
like image 69
Marcin Avatar answered Sep 28 '22 13:09

Marcin