intersect calls ismember . In your case, you do not need all the complex checks that intersect performs, so you can save some overhead and call ismember directly (note: I definitely called both functions before synchronizing them):
a = randi(1000,100,1); b = randi(1000,100,1); >> tic,intersect(a,b),toc ans = 76 338 490 548 550 801 914 930 Elapsed time is 0.027104 seconds. >> tic,a(ismember(a,b)),toc ans = 914 801 490 548 930 550 76 338 Elapsed time is 0.000613 seconds.
You can do this even faster by calling ismembc , a function that does the actual testing, directly. Note that ismembc requires sorted arrays (so you can remove sorting if your input is already sorted!)
tic,a=sort(a);b=sort(b);a(ismembc(a,b)),toc ans = 76 338 490 548 550 801 914 930 Elapsed time is 0.000473 seconds.
Jonas source share