prashanth
prashanth

Reputation: 137

Ranking matrix values in ascending order in matlab

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

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.For zero cell rank1 is given.

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)

Main example

This is my input matrix.

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

expected output matrix is:

 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

Source code i tried:

%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);

Example 1

Input matrix:

  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

Expected output matrix:

 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

Example 2

Input matrix:

 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

Expected output matrix:

 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

Upvotes: 1

Views: 899

Answers (1)

Divakar
Divakar

Reputation: 221564

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

Upvotes: 1

Related Questions