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

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 Comment

How do you have the names stored? You reference that (cell array?) by the order vector.

Sign in to comment.

Answers (1)

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!

Categories

Asked:

on 29 Apr 2014

Answered:

on 21 Jun 2024

Community Treasure Hunt

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

Start Hunting!