reindexing a correlation matrix
12 views (last 30 days)
Show older comments
I have a matrix 100x100.
I would like to re-sort the indices of the elements, and re-create the matrix with the new numbering scheme.
showing this as a 3x3 to simplfy my question:
[1 1 1 ; 2 2 2 ; 3 3 3 ]
So that a vector of the element names is 1,2,3,...100.
I want to rename the elements with a new vector, that looks random: 97,5,6... etc
so that I have a new matrix with the new naming arrangement and
so that the value of original element (1,2) will now be the values that was (97,5)
I hope this makes sense. The matrix is not symmetric across the diagonal.
Can someone help me?
thanks
Teena
3 Comments
Jan
on 23 Apr 2019
@teena dobbs: Of course you can run my code with a specified order also. I used randperm only to produce some meaningful testdata and a valid permutation. Simply insert the wanted permutation in the variable index.
By the way, this resorts the elements, such that the standard indexing method replies teh wanted values. You cannot resort the indices itself. I do not understand your example:
showing this as a 3x3 to simplfy my question:
[1 1 1 ; 2 2 2 ; 3 3 3 ]
So that a vector of the element names is 1,2,3,...100.
Please explain, what the actual problem is and why my suggested code does not solve it.
Answers (1)
Bjorn Gustavsson
on 23 Apr 2019
If you want to reindex a correlation matrix you should maintain the correct correlation-structure - so you have to apply the same permutations to the rows and the collumns. Something like this works:
D = randn(50,5);
C = corrcoef(D);
repermidx = [2 3 1 5 4];
C2 = C(:,repermidx);
C2 = C2(repermidx,:);
Cr = corrcoef(D(:,repermidx));
disp(C2-Cr)
Adjust repermidx to suit your needs.
HTH
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!