Permutation according to table

조회 수: 2 (최근 30일)
lilly lord
lilly lord 2020년 5월 21일
댓글: lilly lord 2020년 5월 21일
Hi, I have two tables
P=[188 5 95 60;3 59 0 111;255 123 51 84];
Ma=[25 222 80 6;100 1 190 97;73 33 254 184];
M_1=[];
for i=1:3
M_1(i,:)=sort(Ma(i,:));
end
M_1;
t1=table(M_1);
t2=table(P);
Now as t1 rows are arranged in ascending order, I wand t2 entries should also permute according to t1. i.e if t1 is arranged in ascending order then the corresponding t2 should also changed
6 correspons to 60 so 60 comes first, 25 corresponds to 188 so 188 comes second ,80 correspond to 95 so 95 comes at third place and so on
t3=[60 188 95 5;59 111 3 0;123 255 84 51]
I there a way to do so. Thanks in advance

채택된 답변

Quad
Quad 2020년 5월 21일
편집: Quad 2020년 5월 21일
Sort has the option to also return the index of the sorted array, so you can sort P the same way. Here is a slight modification to your code
P=[188 5 95 60;3 59 0 111;255 123 51 84];
Ma=[25 222 80 6;100 1 190 97;73 33 254 184];
M_1=zeros(size(Ma));
P_1=zeros(size(P));
for i=1:3
[M_1(i,:),Index]=sort(Ma(i,:));
P_1(i,:) = P(i,Index);
end
M_1;
t1=table(M_1);
t2=table(P_1);
  댓글 수: 1
lilly lord
lilly lord 2020년 5월 21일
Thank u. You saved my time

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by