reindexing a correlation matrix

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

Jan
Jan 2019년 4월 19일
편집: Jan 2019년 4월 19일
You can simply edit the question instead of posting a comment.
I guess, the solution is trivial:
x = reshape(randperm(16)) % Test data
index = randperm(16) % The new order
y = reshape(x(index), size(x))
teena dobbs
teena dobbs 2019년 4월 21일
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 2019년 4월 23일
@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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2019년 4월 23일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

2019년 4월 18일

답변:

2019년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by