How do I compare two shuffled vectors, and get the indexes of one as it appears in the other?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Henry Brice
am 8 Sep. 2022
Kommentiert: Henry Brice
am 8 Sep. 2022
I have two vectors of strings, one is a shuffled version of the other. I want to get a new vector that has the indexes of the elements in the first vector, as they appear in the second.
So, for example, for the following two vectors:
A=["cond1","cond2","cond3","cond4"];
b=["cond4","cond2","cond1","cond3"];
I'd want to get the following output
ans = 3 2 4 1
I.e. telling me that the first element in A is in position 3 in B, the second is in position 2, and so on.
2 Kommentare
Stephen23
am 8 Sep. 2022
"I have two vectors of strings,"
But what you showed us are character vectors. Because square brackets are a concatenation operator, your example data:
A = ['cond1','cond2','cond3','cond4'];
is exactly equivalent to this:
A = 'cond1cond2cond3cond4';
Perhaps you really meant to show us string arrays, not the character arrays that you actually gave us.
Akzeptierte Antwort
Stephen23
am 8 Sep. 2022
A = ["cond1","cond2","cond3","cond4"];
b = ["cond4","cond2","cond1","cond3"];
[~,X] = ismember(A,b)
0 Kommentare
Weitere Antworten (1)
David Hill
am 8 Sep. 2022
b=["cond4","cond2","cond1","cond3"];%needs to be string array
[~,idx]=sort(b)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!