Filter löschen
Filter löschen

Ismember function -- return all indexes, not just lowest?

38 Ansichten (letzte 30 Tage)
Tom
Tom am 12 Jun. 2013
Kommentiert: Captain Karnage am 27 Jun. 2023
According to the Matlab documentation,
[Lia,Locb] = ismember(A,B) returns an array, Locb, containing the lowest index in B for each value in A that is a member of B.
Is there a way to return an array (or matrix) containing all indexes in B for each value in A that is a member of B?

Akzeptierte Antwort

Matt J
Matt J am 12 Jun. 2013
Bearbeitet: Matt J am 12 Jun. 2013
For numeric A and B, I would probably do something like this
Lia = ismember(A,B)
idx=find(Lia);
map=bsxfun(@eq, A(idx),B(:));
Then map(:,i) will be a logical index into B of all points where B equals A(idx(i))
  1 Kommentar
Captain Karnage
Captain Karnage am 27 Jun. 2023
The step idx=find(Lia) is not necessary, you can use A(Lia) to get the same result and it is a faster, more efficient way to index A in MATLAB.
Also, if B is not a vector, this will not work. You would need more steps to search a 2(or more)-D array.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 13 Jun. 2013
Then the output can have different sizes for the different elements of A and a cell is required. What about a simple loop:
Out = cell(1, numel(A))
for iA = 1:numel(A)
Out{iA} = find(B == A(iA));
end

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by