reordering matrix elements according to another matrix

14 ビュー (過去 30 日間)
Lama Hamadeh
Lama Hamadeh 2022 年 4 月 4 日
回答済み: Torsten 2022 年 4 月 4 日
Hi,
If I have the following two matrices:
A = [2 ; 4 ; 1 ; 3];
B = [12; 15; 20; 9];
where each row in B corresponds to each row in A. If I sort the rows in A in an ascending order as:
A = sortrows(A); %matrix A here looks like this now A = [1 ; 2; 3; 4];
How can I reorder matrix B so that each of its rows corresponds to the reordered matrix A. In other words, how can I get the follwoing:
B = [20; 12; 9; 15];
Thanks.

採用された回答

Torsten
Torsten 2022 年 4 月 4 日
[A,index] = sortrows(A)
B = B(index,:)

その他の回答 (1 件)

Stephen23
Stephen23 2022 年 4 月 4 日
編集済み: Stephen23 2022 年 4 月 4 日
A = [2 ; 4 ; 1 ; 3];
B = [12; 15; 20; 9];
[A,X] = sort(A); % or SORTROWS
B = B(X)
B = 4×1
20 12 9 15

カテゴリ

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