Finding the indices of the elements of one array in another

9 visualizzazioni (ultimi 30 giorni)
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 Commento
Deepa Maheshvare
Deepa Maheshvare il 20 Gen 2022
idx = arrayfun( @(x)( find(nodes==x) ), subnodes )
works as expected, but I am not sure why the one posted above doesn't works

Accedi per commentare.

Risposta accettata

Rik
Rik il 20 Gen 2022
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.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by