matlab:对一个向量进行排序,返回每一个数据的rank 序号 。。。

时间:2024-03-02 10:37:18

%% Rank the entropy_loss     
% for iiii = 1:size(Group_age, 1)  
%     count_1 = 0 ;
%     tmp = Group_age(iiii, 1);
%     for jjj = 1:size(Group_age, 1)
%         if (Group_age(jjj, 1) > tmp)
%             count_1 = count_1 + 1;
%         else
%             continue;
%         end
%     end
%     Rank_age(iiii, 1) = count_1 + 1;    
% end

返回的序号绝逼是错误的额。。为何?

利用 matlab sort 函数,就可以了,如:

 

>> X = [3 7 5 0 4 2 ];
>> [y, index] = sort(X)

y =

     0     2     3     4     5     7


index =

     4     6     1     5     3     2