Finding the indices of the elements of one array in another

조회 수: 8 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2022년 1월 20일
편집: Deepa Maheshvare 2022년 1월 20일
Given two vectors A and B,
I want to find the index of elements of B in A
I tried
A = ["G1", "V2", "G3", "G4", "V1"]
B = ["V1", "G4"];
[sharedvals,idxs] = intersect(A, B, 'stable')
The above returns
idxs =
4
5
Expected result:
idxs =
5
4
Could someone suggest how to obtain the expected result?
  댓글 수: 1
Deepa Maheshvare
Deepa Maheshvare 2022년 1월 20일
idx = arrayfun( @(x)( find(nodes==x) ), subnodes )
works as expected, but I am not sure why the one posted above doesn't works

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2022년 1월 20일
You muct have thought of this already (as it is one of your tags), but ismember does what you need:
A = ["G1", "V2", "G3", "G4", "V1"];
B = ["V1", "G4"];
[sharedvals,idxs] = ismember(B,A)
sharedvals = 1×2 logical array
1 1
idxs = 1×2
5 4
If you want a column vector you can transpose it.
  댓글 수: 1
Deepa Maheshvare
Deepa Maheshvare 2022년 1월 20일
편집: Deepa Maheshvare 2022년 1월 20일
Ahh, thanks. I had tried [sharedvals,idxs] = find(ismember(A, B)).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by