How to quickly find the indecis of an array in another array?

4 views (last 30 days)
Dear All,
I have arrays A and B. I want to quickly find out the repeat entries of A in B without using intersect. For example, A = [21 32 33 41 28 91 30], and B = [32 67 41 91 100].
I want to find out the indecies of the entries in B also showing in array A. The solution is C =[1 3 4] whose correspoding entries are 32, 41 and 91 which are also entries in A.
I donot want to use intersect because it is slow in computation.
Thanks.
Benson

Accepted Answer

Jan
Jan on 21 Jun 2021
Edited: Jan on 22 Jun 2021
Do you have such tiny inputs in all cases? Then a linear search is faster than the smart sorting of intersect and the binary search:
X = [21 32 33 41 28 91 30];
Y = [32 67 41 91 100];
tic; for k = 1:1e5; [~, ind] = intersect(Y, X); end; toc
tic; for k = 1:1e5; ind = AinBidxLin(Y, X); end; toc
tic; for k = 1:1e5; ind = AinBidx(Y, X); end; toc
% Matlab R2018b:
% Elapsed time is 7.531878 seconds. % Intersect
% Elapsed time is 0.296715 seconds. % Linear search with loops
% Elapsed time is 0.323204 seconds. % ismembc-Mex
function Ai = AinBidxLin(A, B)
% INPUT: A, B: Vectors, numerical or CHAR. No NaNs.
% OUTPUT: Ai: Indices of elements of A which appear in B.
% Linear search, efficient for small inputs, e.g. < 20 elements.
% AUTHOR: Jan, Heidelberg, CC BY-SA 3.0
nA = numel(A);
M = false(1, nA);
on = true;
B = B(:);
for iA = 1:nA
if ~isempty(find(A(iA) == B, 1))
M(iA) = on;
end
end
Ai = find(M);
end
function Ai = AinBidx(A, B)
% INPUT: A, B: Vectors, numerical or CHAR. No NaNs.
% OUTPUT: Ai: Indices of elements of A which appear in B.
Ai = find(ismembc(A(:), sort(B(:))));
end
If the inputs are not tiny, use the ismembc appraoch. This is the core of intersect also, but unfortunately it is not documented. Maybe this compiled C-Mex function will be removed in future Matlab versions.
[EDITED] Some further tests let me prefer the ismembc approach for all inputs, even very small ones. Please try this by your own.
The set functions intersect, setdiff, union has been accelerated in modern Matlab versions. But at least at Matlab online, ismembc is still 10 times faster.
  4 Comments
Benson Gou
Benson Gou on 22 Jun 2021
Hi, Jan,
I tested your code. I used intersect 3 times in my code. I compared the speed between your code and intersect. I found 2 of them intersect is fsater, but 1 of them your code is much faster. I do not know why.
It seems to me that the 3 times using intersect looks no different.
Thanks a lot.
Benson
Benson Gou
Benson Gou on 22 Jun 2021
Dear Jan,
I tested your code again. I found your code performs very well and saved me about 10 seconds. All three times using your code is faster than intersect.
Thanks a lot.
Benson

Sign in to comment.

More Answers (1)

dpb
dpb on 21 Jun 2021
>> [~,ia]=find(ismember(B,A))
ia =
1 3 4
>>
whether it is any faster I doubt, but it doesn't use intersect.
Whatever has to do the work, not sure there is magic bullet here.
  1 Comment
Benson Gou
Benson Gou on 22 Jun 2021
Hi, dpb,
Your code generated a ia = [ 1 1 1 1 1 1 1 1 1]. It is faster but the indecies are not correct.
Thanks a lot.
Benson

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!