How to other all the matrix based on other vector

2 ビュー (過去 30 日間)
Jose Valles
Jose Valles 2018 年 9 月 11 日
コメント済み: Jose Valles 2018 年 9 月 11 日
How can I order a 2x2 matrix based on a vector For example:
B = [0 0 ; 1 4 ; 2 16 ; 3 19 ; 4 5 ; 5 39 ; 6 4]
C = [3 4 5 6 0 1 2]
the desired output is
D = [3 19; 4 5; 5 39; 6 4; 0 0; 1 4 ; 2 16]
I have been struggling trying to find the correct code line but i am only able to sort the one column and not the second column based on the first one

採用された回答

Rik
Rik 2018 年 9 月 11 日
Matlab is not zero-indexed, hence the +1 below.
B = [0 0 ; 1 4 ; 2 16 ; 3 19 ; 4 5 ; 5 39 ; 6 4] ;
C = [3 4 5 6 0 1];
order=C+1;
B_ordered=B(order,:);
  1 件のコメント
Jose Valles
Jose Valles 2018 年 9 月 11 日
Cool!! it also works very well
I have also submitted a answer

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

その他の回答 (1 件)

Jose Valles
Jose Valles 2018 年 9 月 11 日
Ok ... I Think i got the answer. Here it is
[~,order] = sort(C);
[Bs,~] = sortrows(B,1);
D(order,:) = Bs

カテゴリ

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