How does a matrix sort based on another matrix?

조회 수: 1 (최근 30일)
SM
SM 2020년 2월 12일
댓글: JESUS DAVID ARIZA ROYETH 2020년 2월 12일
I have two matries such as
A=[2 3 1;2 1 3] and B=[0.2 0.15 0.1; 0.3 0.2 0.1]. Now I want to create a matrix which looks like C=[ 0.1 0.2 0.15; 0.2 0.3 0.1]. What actually happened here is that
B(1,1) moves to C(1,2) because of A(1,1)=2 .
B(1,2) moves to C(1,3) because of A(1,2)=3;
B(1,3) moves to C(1,1) beacause of A(1,3)=1;
B(2,1) moves to C(2,2) beacause of A(1,1)=2;
B(2,2) moves to C(2,1) beacause of A(1,2)=1;
B(2,3) moves to C(2,3) beacause of A(1,3)=3;
I used just C=B(A)..it does not make sense for me! Is there any possible solution without loop?

채택된 답변

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2020년 2월 12일
편집: JESUS DAVID ARIZA ROYETH 2020년 2월 12일
Solution:
C=zeros(size(B));
C(sub2ind(size(B),repmat((1:size(B,1))',1,size(B,2)),A))=B
  댓글 수: 2
SM
SM 2020년 2월 12일
The matrix size varies with my problem size...So, n*m should adjust automatically!
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2020년 2월 12일
try now!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by