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

1 view (last 30 days)
Benson Gou on 21 Jun 2021
Commented: Benson Gou on 22 Jun 2021
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

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.
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

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.
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