Extracting the corresponding value of a rank vector in Matlab

青春壹個敷衍的年華 提交于 2019-12-13 09:37:50

问题


I intend to assign ranks to indices of an array (not the array itself) and create a second vector containing these ranks.

Example:

data: x=[ 3 7 2 0 5 2]
ranks: x'=[ 3 5 2 1 4 2]

After assigning the ranks to the indices, I need to extract the corresponding value of the kth (starting from 1) minimum index in each iteration of a loop, until a certain condition satisfies.

For example, I need the corresponding value of the first minimum index in the first iteration, meaning the corresponding value of element "1" in ranks vector, which equals the element "0" in the x vector, while the index of both vectors remain constant (to be clear, each element in the x vector represents a certain property of a user, so if indices change, I'll lose track of the users).

Following this procedure, in the second loop, without considering the first minimum, I need to extract the second minimum element and so on.


回答1:


This kind of question has been asked and answered on many occasions, but I can't seem to find a good duplicate.

Regardless, you would use the second output of sort on the rank vector to give you the position of where each value would appear in the sorted result. Specifically, for each value in the second output of sort, it would tell you which value in the original vector you would have to grab to place it in this corresponding position to sort the data.

You would then use this ordering to index into your data to get the right and desired ordering.

Specifically:

x=[ 3 7 2 0 5 2];
ranks = [3 5 2 1 4 2];

[~,ind] = sort(ranks);
out = x(ind);

We get for out:

out =

     0     2     2     3     5     7

You can verify that we are pulling out values in the data that respect the sorted order seen in ranks.



来源:https://stackoverflow.com/questions/31992090/extracting-the-corresponding-value-of-a-rank-vector-in-matlab

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