# How do I compare two shuffled vectors, and get the indexes of one as it appears in the other?

2 ビュー (過去 30 日間)
Henry Brice 2022 年 9 月 8 日
コメント済み: Henry Brice 2022 年 9 月 8 日
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 件のコメント表示非表示 1 件の古いコメント
Henry Brice 2022 年 9 月 8 日
Thanks, sorry, corrected.

サインインしてコメントする。

### 採用された回答

Stephen23 2022 年 9 月 8 日
A = ["cond1","cond2","cond3","cond4"];
b = ["cond4","cond2","cond1","cond3"];
[~,X] = ismember(A,b)
X = 1×4
3 2 4 1

サインインしてコメントする。

### その他の回答 (1 件)

David Hill 2022 年 9 月 8 日
b=["cond4","cond2","cond1","cond3"];%needs to be string array
[~,idx]=sort(b)
idx = 1×4
3 2 4 1

サインインしてコメントする。

### カテゴリ

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by