在Matlab中按升序排列矩阵值 [英] Ranking matrix values in ascending order in matlab

查看:369
本文介绍了在Matlab中按升序排列矩阵值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的矩阵R = {r(i,j)},以下是对输入矩阵进行的3个操作:

For given matrix R={r(i,j)} following are the 3 operations done for input matrix:

1.我们将对角线值按升序排列(即给定小数值等级1和下一个小数值等级2等等)

1.We rank diagonal values in ascending order.(i.e for small value rank 1 is given and for next small value rank2 and so on)

2.对于零单元格,给定rank1.

2.For zero cell rank1 is given.

3.除对角线单元和零单元格外,我们将其余值按升序进行排序(零单元格的数量将设置为初始排名顺序,即零单元格排名第一)

3.Other than diagonal cell and zero cell we sort rest values in ascending order.(Number of zero cells will be set as initial rank order i.e zero cells rank first)

主要示例

这是我的输入矩阵.

0.6667    0.1667    0.1667         0    0.6667
0.1667    0.1667    0.1667         0    0.1667
0.1667    0.1667    0.1667         0    0.1667
     0         0         0         0         0
0.6667    0.1667    0.1667         0    1.0000

预期的输出矩阵为:

 4     2     3     1     4
 4     3     3     1     2
 4     3     2     1     2
 1     1     1     1     1
 4     3     2     1     5

但是我得到了我的代码的输出矩阵:

But i got this output matrix for my code:

 4     1     4     0     2
 3     2     4     0     1
 3     4     3     0     1
 0     0     0     0     0
 4     3     2     0     5

我尝试过的源代码:

%to display ordinal graph
E = logical(eye(size(table1)));
% create a mask for the two different rules
% rule 1: diagonal elements first
table2 = zeros(size(table1)); % create result matrix
[~,jj] = sort(table1(E));
[~,ii] = sort(jj);
table2(E) = ii; % assign rank of diagonal elements
% rule 2: rest of the matrix
E = ~E;
B = reshape(table1(E),size(table1,1)-1,size(table1,2))'; % B is the matrix of table1 without diagonal elements
[~,jj] = sort(B,2); % sort along column dimension, 
[~,ii] = sort(jj,2);
table2 = table2'; % matlab is column-major, so you have to transpose the dest matrix before putting in the elements
table2(E) = reshape(ii',[],1);
table2 = table2'; % transpose back, done.
% treat zeros apart: 0 has rank 0
table2(table1==0) = 0;
            disp(table2);

示例1

输入矩阵:

  12  6  6  4  12

  6   6  6  4   6

  6   6  6  4   6

  4   4  4  4   4

  12  6  6  4  16

预期的输出矩阵:

 4  3  2  1  4
 4  3  2  1  3
 4  3  2  1  2
 4  3  2  1  1
 4  3  2  1  5

示例2

输入矩阵:

 10   11  0  13  14
 14    9  8  20   7
 20   25  22 18  13
 16    8  9  23  19
 15    0  0  16  21

预期的输出矩阵:

 2  2  1  3  4
 3  1  2  4  1
 3  4  4  2  1
 3  1  2  5  4
 2  1  1  3  3

推荐答案

在处理对相同值进行排序时,似乎在问题中呈现索引的方式存在问题,但是假设这不会影响最终结果,可以尝试使用5x5矩阵-

It seems there are issues with the way indices are presented in the question when dealing with sorting identical values, but assuming this won't affect the end results, you may try this for 5x5 matrices -

%%// A is your input matrix

%%// Pre-allocate output matrix
out = zeros(size(A));

%%// Take care of operation #3
for k = 1:5
    [~,ind2] = sort(A(k,:))
    ind2(ind2)=1:5;
    out(k,:) = ind2;
end
out = out-bsxfun(@gt,out,diag(out))

%%// Take care of assigning diagonal elements
[~,ind1] = sort(diag(A))
ind1(ind1)=1:5
out(1:size(out,1)+1:end)=ind1

这篇关于在Matlab中按升序排列矩阵值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆