How to rank data and set corresponsing values

I have data, 10 columns and over 200 rows. First two rows are:
0,004130861 0,001492476 -0,001031031 0,0033182 0,000327482 0,000510943 0,00083309 0,001345621 -0,002916075 6,51063E-05
0,003489052 0,001341031 -0,000738983 0,00290706 0,0001296 0,001039578 0,00099295 0,001224308 -0,002254135 -8,60491E-05
I need to rank the data from 1 - 10, so it becomes
1 3 9 2 7 6 5 4 10 8
1 3 9 2 7 5 6 4 10 8
and then set lowest value here to 1 and highest value to -1, seeting all other to 0, so it becomes
-1 0 0 0 0 0 0 0 1 0
-1 0 0 0 0 0 0 0 1 0
And so on for all other rows
How do i code this?

1 Comment

What does "rank" mean in your problem? I do not see the input to output relationship in the problem statement. Are you sorting them from high to low, or low to high, along each row? Then the final output has one +1, one -1, and the rest are zeros, where +1 means the max and -1 means the min.

Sign in to comment.

Answers (1)

Assuming the rankings are just used to explain your final goal, the following seems to work:
M1 = [0.004130861 0.001492476 -0.001031031 0.0033182 0.000327482 0.000510943 0.00083309 0.001345621 -0.002916075 6.51063E-05; 0.003489052 0.001341031 -0.000738983 0.00290706 0.0001296 0.001039578 0.00099295 0.001224308 -0.002254135 -8.60491E-05];
M2 = zeros(size(M1));
[~, idxMin] = min(M1, [], 2);
[~, idxMax] = max(M1, [], 2);
M2(:,idxMin) = 1;
M2(:,idxMax) = -1;
M1 % initial matrix
M1 = 2×10
0.0041 0.0015 -0.0010 0.0033 0.0003 0.0005 0.0008 0.0013 -0.0029 0.0001 0.0035 0.0013 -0.0007 0.0029 0.0001 0.0010 0.0010 0.0012 -0.0023 -0.0001
M2 % result
M2 = 2×10
-1 0 0 0 0 0 0 0 1 0 -1 0 0 0 0 0 0 0 1 0

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Release

R2021b

Tags

Asked:

on 14 Mar 2022

Edited:

on 14 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!