Sort Matrix according to another Vector

I have two sets of data such as:
A = [1 3 4 7 2]';
B = [3 4 7 1 2; 1 2 3 4 5; 6 7 8 9 10]';
Since B first column has the same values as A, but in different order, my question is if it is possible to sort B in a way that the resulting matrix is:
B = [1 3 4 7 2; 4 1 2 3 5; 9 6 7 8 10]';

 採用された回答

Voss
Voss 2022 年 10 月 8 日

2 投票

A = [1 3 4 7 2]';
B = [3 4 7 1 2; 1 2 3 4 5; 6 7 8 9 10]';
[~,idx] = ismember(A,B(:,1));
B = B(idx,:)
B = 5×3
1 4 9 3 1 6 4 2 7 7 3 8 2 5 10

2 件のコメント

Eduardo Hulse
Eduardo Hulse 2022 年 10 月 8 日
Thank you
Voss
Voss 2022 年 10 月 8 日
You're welcome!

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

その他の回答 (1 件)

Atsushi Ueno
Atsushi Ueno 2022 年 10 月 8 日

1 投票

A = [1 3 4 7 2]';
B_origin = [3 4 7 1 2; 1 2 3 4 5; 6 7 8 9 10]';
% Since B first column has the same values as A, but in different order,
% my question is if it is possible to sort B in a way that the resulting matrix is:
B_corrct = [1 3 4 7 2; 4 1 2 3 5; 9 6 7 8 10]';
[~,Locb] = ismember(A,B_origin(:,1))
Locb = 5×1
4 1 2 3 5
B_origin(Locb,:)
ans = 5×3
1 4 9 3 1 6 4 2 7 7 3 8 2 5 10
B_origin(Locb,:) == B_corrct
ans = 5×3 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2 件のコメント

Eduardo Hulse
Eduardo Hulse 2022 年 10 月 8 日
Thank You
Atsushi Ueno
Atsushi Ueno 2022 年 10 月 9 日
You're welcome!

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

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2022 年 10 月 8 日

コメント済み:

2022 年 10 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by