Borrar filtros
Borrar filtros

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

7 visualizaciones (últimos 30 días)
soni
soni el 29 de Abr. de 2014
Respondida: Ronit el 21 de Jun. de 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 comentario
dpb
dpb el 29 de Abr. de 2014
How do you have the names stored? You reference that (cell array?) by the order vector.

Iniciar sesión para comentar.

Respuestas (1)

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

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by