Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rearranging a Correlation Matrix

540 views
Skip to first unread message

Gerard Middleton

unread,
Oct 22, 1996, 3:00:00 AM10/22/96
to

Here's one for all you expert vectorizers: what if I want to rearrange
the order of the variables in a correlation matrix (for example, from
1,2,3,4,5,6 to 2,5,4,6,3,1), of course without recomputing it. I have
written a function that does this using loops (well, actually, one that
swaps pairs of variables) but I cannot seem to figure how this might be
vectorized.
--
Gerry Middleton
Department of Geology, McMaster University
Tel: (905) 525-9140 ext 24187 FAX 522-3141

Bradley Jones

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to

In article <54jd1d$6...@mcmail.CIS.McMaster.CA>,
midd...@mcmail.cis.McMaster.CA (Gerard Middleton) wrote:

For the example above let C be the old correlation matrix, then:

idx = [2 5 4 6 3 1];
[xidx, yidx] = meshgrid(idx);
midx = xidx + length(idx)*(yidx-1);
newC = C(midx);

Hope this is useful.

--
==== Brad Jones ========================= br...@mathworks.com ======
| The MathWorks, Inc. in...@mathworks.com |
| 24 Prime Park Way http://www.mathworks.com |
| Natick, MA 01760-1500 ftp.mathworks.com |
==== Tel: 508-653-1415 ==== Fax: 508-653-6971 =====================

Gerard Middleton

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to

Besides the solution to my problem provided by The Mathworks, I have
received a much simpler one from Wladimir Shukowsky in Brazil.
If the required order is p = [2,5,4,6,3,1] he suggests simply

C2 = C(p,p)

This works! so does the solution provided by The MathWorks, but I cannot
understand why! Can anyone explain why, and why such a complicated
solution was given when such a simple one works! (please, I am not
ungrateful here, just wondering if there is some deeper reason I have missed)

Wladimir Shukowsky

unread,
Oct 23, 1996, 3:00:00 AM10/23/96
to Gerard Middleton


On 22 Oct 1996, Gerard Middleton wrote:

>
>Here's one for all you expert vectorizers: what if I want to rearrange
>the order of the variables in a correlation matrix (for example, from
>1,2,3,4,5,6 to 2,5,4,6,3,1), of course without recomputing it.

>[snip]


>--
>Gerry Middleton
>Department of Geology, McMaster University
>Tel: (905) 525-9140 ext 24187 FAX 522-3141
>
>

If C is your correlation matrix and p=[2,5,4,6,3,1] is the new
ordering of the variables, then the reordered correlation matrix
is

C_reord = C(p,p)

Hope it helps.

--

+=Wladimir Shukowsky==========wlad...@iag.usp.br=+
| Universidade de Sao Paulo | Tel: +55-11-8184776 |
| Sao Paulo SP / Brazil | Fax: +55-11-8185034 |
+=========WGS84: 23 33'31"S 46 43'59"W===========+

Dave Goodmanson

unread,
Oct 25, 1996, 3:00:00 AM10/25/96
to

Gerry--

I think you will have to be careful because for an arbitrary matrix the
two methods you have been given seem to disagree. The answers are
transposes of each other. This will not matter if the matrices you use
are symmetric about the main diagonal, but it will matter a lot
otherwise. I don't know much about correlation matrices. Are they
symmetric? Even if they are it would probably be best to have the right
solution in case you use the method some other time.

Clearly Wladimir Shukowsky is good with Matlab index conventions. His
solution works because the expression Cnew = C([a b c...],[d,e,f...])
reads rows of C into the new matrix in the order a,b,c.... For each
row, the column entries are put into d,e,f... order. (Alternatively,
you could say that columns of C are read into the new matrix in the
order d,e,f..., and for each column the row entries are put into
a,b,c... order.) This is explained fairly well on pages 2-26 through
2-28 of the Version 4 user's guide. When [a,b,c...] is the same as
[d,e,f...] this is just what you want, because both rows and columns are
interchanged in the same manner.

Brad Jones's method uses the fact that if you create a matrix "midx" of
numbers between (in the 6x6 case) 1 and 36, then C(midx) is a new matrix
with each number in midx replaced by an element of C. The elements are
read out columnwise, e.g. 6 is replaced by the element of C at the
bottom of the first column, 7 by the element of C at the top of the
second column, and 36 by the element of C at the bottom of the last
column. Of course, the trick is to make the right midx matrix. Brad
Jones must be pretty good with Matlab indices too, and I suspect the
reason he didn't send you the simple solution is just that it didn't
occur to him at the time. If it had, I'm sure he would have.

As to which solution is correct, I'm not sure enough about correlation
matrices to say for sure, but certainly in linear algebra terms, if you
have (using an abbreviated example)

y1 x1
y2 = C x2
y3 x3

and you want

y2 x2
y3 = Cnew x3
y1 x1

then the Shukowsky Cnew is correct, and the Jones Cnew is the transpose
of that.

Dave G.


Gerard Middleton wrote:
>
> Besides the solution to my problem provided by The Mathworks, I have
> received a much simpler one from Wladimir Shukowsky in Brazil.
> If the required order is p = [2,5,4,6,3,1] he suggests simply
>
> C2 = C(p,p)
>
> This works! so does the solution provided by The MathWorks, but I cannot
> understand why! Can anyone explain why, and why such a complicated
> solution was given when such a simple one works! (please, I am not
> ungrateful here, just wondering if there is some deeper reason I have missed)

0 new messages