How do I identify elements in a table/matrix and sort them in descending order?
1 view (last 30 days)
Show older comments
Hello!
I've got an matrix/table with given names to each row and column (1,2 ... 10).
Each element has a diffrent number, for example the element N of Row 1 and Column 4 is 52, and Row 4 and Column 7 is 22 and so on.
I want to identify for each (r,c) element number and then put the results in a table where it identifes like this:
(1,4) = 52
(4,7) = 42
and so on...
The purpose is to do this with all types of possible (r,c) and each element and then rank them in descending order.
Any tips?
Example:
1 2 3 4 5 6 7 8 9 10 (col)
1 N N N N N N N N N N
2 N N N N N N N N N N
3 N N N N N N N N N N
4 N N N N N N N N N N
5 N N N N N N N N N N
6 N N N N N N N N N N
7 N N N N N N N N N N
8 N N N N N N N N N N
9 N N N N N N N N N N
10 N N N N N N N N N N
(row)
0 Comments
Answers (1)
Voss
on 21 Oct 2023
Edited: Voss
on 21 Oct 2023
M = magic(5) % some matrix
[v,idx] = sort(M(:),'descend');
[r,c] = ind2sub(size(M),idx);
result = [r, c, v]
2 Comments
Voss
on 21 Oct 2023
T = array2table(magic(5)); % some table with row and variable/column names
T.Properties.RowNames = num2cell('A':'E');
T.Properties.VariableNames = num2cell('J':'N');
disp(T)
[v,idx] = sort(T{:,:}(:),'descend');
[r,c] = ind2sub(size(T),idx);
row_names = T.Properties.RowNames(:);
col_names = T.Properties.VariableNames(:);
result = table(row_names(r), col_names(c), v)
See Also
Categories
Find more on Tables 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!