http://www.mathworks.com/matlabcentral/newsreader/view_thread/307556
MATLAB Central Newsreader  Correlation matrix, efficient method
Feed for thread: Correlation matrix, efficient method
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 10 May 2011 17:23:04 +0000
Correlation matrix, efficient method
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307556#835302
susan
hello,<br>
<br>
I have a matrix of x* y* z *176 from a fMRI imaging study.<br>
<br>
I need to get a square matrix with correlation values. i.e. each 'voxel(x*y*z)' has a timeseries (176). I need to correlate this with every other voxel.<br>
<br>
There has to be a more efficient way than for loops. So please help.<br>
<br>
Thanks,<br>
S

Tue, 10 May 2011 18:55:23 +0000
Re: Correlation matrix, efficient method
http://www.mathworks.com/matlabcentral/newsreader/view_thread/307556#835312
Roger Stafford
"susan" wrote in message <iqbs9o$mf6$1@newscl01ah.mathworks.com>...<br>
> hello,<br>
> <br>
> I have a matrix of x* y* z *176 from a fMRI imaging study.<br>
> <br>
> I need to get a square matrix with correlation values. i.e. each 'voxel(x*y*z)' has a timeseries (176). I need to correlate this with every other voxel.<br>
> <br>
> There has to be a more efficient way than for loops. So please help.<br>
> <br>
> Thanks,<br>
> S<br>
        <br>
If your matrix is four dimensional of x by y by z by 176 size and if x, y, and z are large, not only is it a very large matrix, but the square correlation matrix you desire would have to be a whopping big (x*y*z)^2 in size! I hope your computer has room for such a monstrous entity.<br>
<br>
In any case, my recommendation would be to treat each of voxels' individual time series separately in such a way that its sum is zero and the sum of its squares is one. Then all the correlation values can be calculated as the sum of the various paired products, which is a much simpler computation than doing a correlation calculation for each such pair from the original values.<br>
<br>
If v is one of these voxel time series, subtract mean(v) from each element, getting v1. Then divide each v1 element by the square root of the sum of its squares, getting v2. Then the sum of the v2 will be zero and the sum of its squares will be one. The time spent doing this in comparison to your main task will be minuscule. <br>
<br>
If V is a matrix of (x*y*z) by 176 size containing these adjusted v2 quantities, then you can get your desired correlation matrix by simple matrix multiplication:<br>
<br>
R = V*V.';<br>
<br>
However, this will be necessarily be extremely timeconsuming and memoryfilling, even with the above simplifications.<br>
<br>
A short cut would be to replace this matrix multiplication by forloops that only handle each possible pair once rather than twice, which would cut the time down by half though using slower forloop operations. You would still have the problem of storage space.<br>
<br>
Roger Stafford