How to sort matrix based on another matrix?

13 ビュー (過去 30 日間)
K Venkatesh
K Venkatesh 2019 年 8 月 26 日
コメント済み: K Venkatesh 2019 年 8 月 26 日
A=[3,2,1;8,3,1;6,4,2] and B=[5,9,2;9,2,1;8,7,4] Matrix A sort by row like A=[1,2,3;1,3,8;2,4,6] then based on A change values of B matrix like B=[2,9,5;1,9,2;4,7,8].

採用された回答

Stephen23
Stephen23 2019 年 8 月 26 日
>> A = [3,2,1;8,3,1;6,4,2]
A =
3 2 1
8 3 1
6 4 2
>> B = [5,9,2;9,2,1;8,7,4]
B =
5 9 2
9 2 1
8 7 4
>> [A,X] = sort(A,2);
>> for k = 1:size(X,2), B(k,:) = B(k,X(k,:)); end
>> A
A =
1 2 3
1 3 8
2 4 6
>> B
B =
2 9 5
1 2 9
4 7 8
  1 件のコメント
K Venkatesh
K Venkatesh 2019 年 8 月 26 日
Thank you very much Stephen

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

その他の回答 (1 件)

Rik
Rik 2019 年 8 月 26 日
Assuming you made a small typo in your B_out:
A=[3,2,1;8,3,1;6,4,2];
B=[5,9,2;9,2,1;8,7,4];
for row=1:size(A,1)
[A(row,:),order]=sort(A(row,:));
B(row,:)=B(row,order);
end
clc,disp(B)
  1 件のコメント
K Venkatesh
K Venkatesh 2019 年 8 月 26 日
Thank you very much Rik

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by