- Extract the diagonal elements of the matrix.
- While extracting the diagonal elements, also store their original indices. This is crucial because you will need to map the sorted diagonal elements back to their original positions in the matrix.
- Sort the extracted diagonal elements in ascending (or descending) order. Sorting will help you determine the ordinal ranks of these elements.
- After sorting, assign ranks to the diagonal elements based on their order in the sorted list. The highest element gets the rank of 5 in this case, the next highest gets the rank of 4, and so on.
Matlab code for Sorting diagonal elements of matrix and index of sorted elements is filled into new matrix as shown below?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
This first matrix table1 contains normalized values for 5 names.I need to perform some operations on this matrix and I have to obtain second matrix as shown in table2.
Diagonal elements of table2 should obtained by giving rank(ordinal value) to each value.That means highest element is given 5th rank and next highest is given 4th 3rd and so on.
OPERATION:For diagonal elements
B(1,1)=5(first largest element)
B(2,2)=1(5th largest element)
B(3,3)=4(4th largest element)
B(4,4)=2(2nd largest element)
B(5,5)=3(3rd largest element)
Table1:
BASAVARAJ MANOJ NATESH VIJAY GOWDA
BASAVARAJ 1.0000 0.2727 0.3182 0.0455 0.2727
MANOJ 0.2727 0.2727 0 0 0
NATESH 0.3182 0 0.4545 0.1818 0
VIJAY 0.0455 0 0.1818 0.2727 0.0909
GOWDA 0.2727 0 0 0.0909 0.3636
Table2:
BASAVARAJ MANOJ NATESH VIJAY GOWDA
BASAVARAJ 5 0 0 0 0
MANOJ 0 1 0 0 0
NATESH 0 0 4 0 0
VIJAY 0 0 0 2 0
GOWDA 0 0 0 0 3
0 comentarios
Respuestas (1)
ag
el 17 de Jul. de 2024
Hi Prashanth,
To assign ranks based on the ordinal value of the diagonal elements in a matrix, you can follow the steps outlined below:
The below MATLAB code snippet demonstrates the process:
%data
table1 = [1.0000, 0.2727, 0.3182, 0.0455, 0.2727;
0.2727, 0.2727, 0, 0, 0;
0.3182, 0, 0.4545, 0.1818, 0;
0.0455, 0, 0.1818, 0.2727, 0.0909;
0.2727, 0, 0, 0.0909, 0.3636];
table1Size = size(table1);
%extracting the diagonal elements and storing the original indices for the same
diags = [diag(table1) [1:table1Size(1)]'];
%sorting the diagonal elements in descending element
diags = sortrows(diags, 'descend');
%initializing rank with the size of the matrix
rank = table1Size(1);
table2 = zeros(table1Size);
for i = 1 : table1Size(1)
position = diags(i, 2); %extracting the original position of the diagonal element
table2(position, position) = rank; %assigning the rank
rank = rank - 1; %decreamenting the rank for the next diagonal element
end
disp(table2)
Hope this helps!
0 comentarios
Ver también
Categorías
Más información sobre Operating on Diagonal Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!