MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Efficient construction of a correlation matrix

On 4/9/09 at 5:54 AM, google at (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]]

  • Prev by Date: Re: combining contourplot and regionplot [Addendum]
  • Next by Date: Re: Re: Fast Access to Installed Packages
  • Previous by thread: Re: Efficient construction of a correlation matrix
  • Next by thread: Re: Efficient construction of a correlation matrix