"susan" wrote in message <iqbs9o$mf6$1@newscl01ah.mathworks.com>...
> hello,
>
> I have a matrix of x* y* z *176 from a fMRI imaging study.
>
> 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.
>
> There has to be a more efficient way than for loops. So please help.
>
> Thanks,
> S
        
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.
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.
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.
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:
R = V*V.';
However, this will be necessarily be extremely timeconsuming and memoryfilling, even with the above simplifications.
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.
Roger Stafford
