find sorting index per row of a 2D matrix in MatLab and populate a new matrix

帅比萌擦擦* 提交于 2019-12-11 04:10:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!