Re: Efficient construction of a correlation matrix
- To: mathgroup at smc.vnet.net
- Subject: [mg98481] Re: Efficient construction of a correlation matrix
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 10 Apr 2009 04:58:18 -0400 (EDT)
On 4/9/09 at 5:54 AM, google at dantimatter.com (dantimatter) wrote: >This was a question that was asked back in 2003 that didn't seem to >have a response: is there a fast way to generate a matrix of >correlation coefficients given a list of vectors? I have about >fifty vectors that are each around 1300 elements long and I'd like >to correlate them with each other to get a 50x50 matrix. I know >that I can do this with loops or tables but I thought that perhaps >there exists a fast and computationally inexpensive way to do >this. Any thoughts? Here is a simple way to accomplish what you want without using loops or tables: In[1]:= data = RandomReal[1, {5, 5}]; Partition[Correlation @@@ Tuples[Transpose@data, 2], 5] Out[2]= {{1., -0.56854, -0.304609, 0.829355, -0.137128}, {-0.56854, 1., 0.844872, -0.668716, 0.220838}, {-0.304609, 0.844872, 1., -0.205393, 0.00860996}, {0.829355, -0.668716, -0.205393, 1., -0.41979}, {-0.137128, 0.220838, 0.00860996, -0.41979, 1.}} I transposed the data since Tuples will generate all pairs of rows in the data matrix. And Partition is used since Tuples generates a flat list (length = 25) of pairs rather than a n x n array. Note, while this is simple, it is not the most computationally efficient method since it does more than twice the amount of work needed. All elements of the resulting matrix with indices n,m will be one when n=m and element[[n,m]] will always equal element[[m,n]]