Sorting correlation matrix- with row and column information - need help..
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I am trying to sort a correlation matrix (high to low)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/161856/image.png)
using
MeanCorrMatPerm=mean(data);
[sortedCorr indPerm]=sort(MeanCorrMatPerm,'descend');
imagesc(data(indPerm,indPerm));
When I am sorting only numerical values I am getting results, but I don't know how to get results with row and column names( Altered rows and column names after sorting). Please help me with this problem. Thank you.
1 commentaire
dpb
le 29 Avr 2014
How do you have the names stored? You reference that (cell array?) by the order vector.
Réponses (1)
Ronit
le 21 Juin 2024
Hello Soni,
I understand you are trying to sort a correlation matrix along with its row and column names. Below is a solution that addresses this issue effectively:
% Define the correlation matrix and row/column names
data = [1 0.799 0.193 0.298 -0.111;
0.799 1 0.153 0.218 -0.172;
0.193 0.153 1 0.036 0.086;
0.298 0.217 0.036 1 0.333;
-0.111 -0.171 0.086 0.333 1];
names = {'a', 'b', 'c', 'd', 'e'};
% Compute the mean of the correlation matrix
MeanCorrMatPerm = mean(data);
% Sort the mean values in descending order
[sortedCorr, indPerm] = sort(MeanCorrMatPerm, 'descend');
% Reorder the matrix and the row/column names
sortedData = data(indPerm, indPerm);
sortedNames = names(indPerm);
% Display the reordered matrix with the new row and column names
imagesc(sortedData);
set(gca, 'XTick', 1:length(sortedNames), 'XTickLabel', sortedNames);
set(gca, 'YTick', 1:length(sortedNames), 'YTickLabel', sortedNames);
colorbar;
This script will compute the correlation matrix, sort it, and reorder the row and column names accordingly, providing you with a neatly sorted correlation matrix.
Hope it helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!