How do I identify elements in a table/matrix and sort them in descending order?
Mostrar comentarios más antiguos
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)
Respuestas (1)
M = magic(5) % some matrix
[v,idx] = sort(M(:),'descend');
[r,c] = ind2sub(size(M),idx);
result = [r, c, v]
2 comentarios
Aron
el 21 de Oct. de 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)
Categorías
Más información sobre Aerospace Blockset en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!