How can I number each element of a matrix according to the corresponding ordered position, row by row?

조회 수: 3 (최근 30일)
For example, I have matrix A:
A = [9 15 1 16
15 9 1 16
1 6 2 4]
The sorted matrix is:
B= [1 9 15 16
1 9 15 16
1 2 4 6]
For each rows of A, I want to determine if each element corresponds to the first, second, third or fourth position, as follows:
A'= [2 3 1 4
3 2 1 4
1 4 2 3]
Thanks for help

채택된 답변

Andrei Bobrov
Andrei Bobrov 2019년 1월 8일
편집: Andrei Bobrov 2019년 1월 8일
[~,ii] = sort(A,2);
[~,out] = sort(ii,2)

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 8일
편집: madhan ravi 2019년 1월 8일
Note: Your A' doesn't correspond to the exact position. See https://www.mathworks.com/help/matlab/ref/sort.html#bt8nojg-1-I
A = [9 15 1 16 ;...
15 9 1 16 ;...
1 6 2 4 ];
Adash = [2 3 1 4 ;...
3 2 1 4 ;...
1 4 2 3];
[B,I]=sort(A,2) % B - sorted matrix , I - position of the original values --> see doc sort for explanation
Gives:
B =
1 9 15 16
1 9 15 16
1 2 4 6
I =
3 1 2 4
3 2 1 4
1 3 4 2
To compare A' you could do somethig like below:
Adash==I
Gives:
ans =
3×4 logical array
0 0 0 1
1 1 1 1
1 0 0 0
  댓글 수: 1
Daniela Conforti
Daniela Conforti 2019년 1월 8일
This is not what I want to achieve. I need the opposite corrispondence. If we have, for example, only the vector A= [9 15 1 16] the sorted vector is B= [1 9 15 16]; If we look at B, 9 is the second element, 15 is the third, 1 is the first and 16 is the forth. I want to obtain A' --> [2 3 1 4], but for all rows.

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

카테고리

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