Filter löschen
Filter löschen

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

7 Ansichten (letzte 30 Tage)
soni
soni am 29 Apr. 2014
Beantwortet: Ronit am 21 Jun. 2024
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 Kommentar
dpb
dpb am 29 Apr. 2014
How do you have the names stored? You reference that (cell array?) by the order vector.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ronit
Ronit am 21 Jun. 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!

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by