sorting - Ranking diagonal values of matrix in ascending order and other than diagonal values depending on diagonal values in matlab -
this question has answer here:
input matrix:
1.0000 0 0.2173 0.2585 0.3764 0 0 0 0 0 0.2173 0 0.2173 0.2173 0.2173 0.2585 0 0.2173 0.2585 0.2585 0.3764 0 0.2173 0.2585 0.3764
expected output matrix:
5 1 2 3 4 1 1 1 1 1 2 1 2 2 2 3 1 2 3 3 4 1 2 3 4
for given matrix r={r(i,j)} following 2 operations done input matrix:
1.we rank diagonal values in ascending order.(i.e small value rank 1 given , next small value rank2 , on)
ex: 0-1st rank 0.2173-2nd rank 0.2585-3rd rank 0.3764-4th rank 1.0000-5th rank
2.other diagonal cell sort rest values rowise depending on diagonal value.(i.e diagonal value rank given other matrix values)
ex:for 1st row 2nd row 3rd row 4th row 5th row 0-1st rank 0-1st rank 0-1st rank 0-1st rank 0-1st rank 0.2173-2nd rank 0-1st rank 0.2173-2nd rank 0.2173-2nd rank 0.2173-2nd rank 0.2585-3rd rank 0-1st rank 0.2173-2nd rank 0.2585-3rd rank 0.2585-3rd rank 0.3764-4th rank 0-1st rank 0.2173-2nd rank 0.2585-3rd rank 0.3764-4th rank
source code tried:
out = zeros(size(table1)); k = 1:5 [~,ind2] = sort(table1(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(table1)); ind1(ind1)=1:5; out(1:size(out,1)+1:end)=ind1; c=out
output matrix got:
5 1 2 3 4 1 1 2 3 4 2 1 2 3 4 3 1 2 3 4 4 1 2 3 4
assuming values present in diagonal... need replace values positive integers whilst maintaining ordering of numbers...
the third output of unique give column vector of values in replacing lowest value 1, second lowest 2 , on... can reshaped matrix of size a
code
[~,~,temp]=unique(a); out=reshape(temp,size(a))
result
out = 5 1 2 3 4 1 1 1 1 1 2 1 2 2 2 3 1 2 3 3 4 1 2 3 4
Comments
Post a Comment