reindexing a correlation matrix

12 views (last 30 days)
teena dobbs
teena dobbs on 18 Apr 2019
Answered: Bjorn Gustavsson on 23 Apr 2019
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
teena dobbs
teena dobbs on 21 Apr 2019
Hi
Thanks for the information, but I should have been clearer. And thanks for letting me know about the edit option - did not see it after my original post.
I need to re-index the matrix, not to an actual random order but to a specific order.
Thanks for your help.
Jan
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.

Sign in to comment.

Answers (1)

Bjorn Gustavsson
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

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!