How to rank data and set corresponsing values
1 view (last 30 days)
Show older comments
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
Benjamin Thompson
on 14 Mar 2022
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.
Answers (1)
Scott MacKenzie
on 14 Mar 2022
Edited: Scott MacKenzie
on 14 Mar 2022
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
M2 % result
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!