1
votes

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
1
Could you elaborate more on the third part - 3.Other than diagonal cell ..? That part isn't clear to me with respect to the expected values even.Divakar
I think it would make some sense if the first row of expected matrix has to be - 3 2 3 0 4 instead.Divakar
Ya you are right.Sorry for typing mistake.prashanth
Consider zero as first rank and next small value as 2nd rank and next small value as 3rd rank and so onprashanth
When MATLAB sorts (at least with the default values), if it encounters identical values, it keeps the first identical at the start and so on. Now in your One more example #1, as the input you have as the fourth row 4 4 4 4 4 and your output for it is 4 3 2 1 1. Using MATLAB it would be 1 2 3 1 4. So it appears you are doing something different. How are you even getting these expected outputs? By hand calculations?Divakar

1 Answers

1
votes

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