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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by