问题
I have a challenge to order my matrix. The provided functions like sortrows
work in the opposite way...
Take this 2D matrix
M =
40 45 68
50 65 58
60 55 48
57 67 44
,
The objective is to find matrix O
that indicates the sorting index (rank) per row, i.e.:
O =
1 2 3
1 3 2
3 2 1
2 3 1
.
So for the second row 50
is the smallest element (1), 65
the largest (3), and 58
is the second largest (2), therefore row vector [1 3 2]
.
回答1:
[~,sorted_inds] = sort(M,2);
will do.
回答2:
I think you're looking for the second output of the regular sort
function:
[~,I] = sort(M,2)
This syntax supresses the actual sorted matrix Msorted
, and returns the indices I
such that
for j = 1:n, Msorted(j,:) = M(I(j,:),j); end
Type doc sort
for more information.
来源:https://stackoverflow.com/questions/12112677/find-sorting-index-per-row-of-a-2d-matrix-in-matlab-and-populate-a-new-matrix