How to quickly find the indecis of an array in another array?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Jan
il 21 Giu 2021
Modificato: Jan
il 22 Giu 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 Commenti
Più risposte (1)
dpb
il 21 Giu 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.
Vedere anche
Categorie
Scopri di più su Search Path in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!