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"?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With