Sorting correlation matrix- with row and column information - need help..

5 次查看(过去 30 天)
Hi, I am trying to sort a correlation matrix (high to low)
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 个)

Ronit
Ronit 2024-6-21
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!

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by