correlation for multi-dimensional arrays

Hi everyone,
I need to compute correlation coefficients - lots of them. I have two three-dimensional arrays (frequency x time x observations) and I want to compute correlations between the two arrays along the third dimension. The result I need is a two-dimensional array of correlation coefficients (frequency x time). If I understand the corr function correctly, corr is only for column vectors. If I loop over my other two dimensions, I can of course compute the correlation for each time-frequency point separately, but this is very slow.
Is there a way to compute correlation coefficients for multi-dimensional arrays along an arbitrary dimension, or any other way to speed up the computation of correlations?
Thanks!

1 件のコメント

Raj
Raj 2014 年 8 月 27 日
Can you please provide formulas (math models) for the multi-dimensional correlation?

サインインしてコメントする。

 採用された回答

David Young
David Young 2011 年 9 月 15 日

3 投票

If you don't have NaNs in the data, and you want the standard Pearson coefficient, then you could try applying the formula for correlation directly, like this:
% Synthetic data for testing
a = rand(10, 10, 100);
b = rand(10, 10, 100);
b(1, 1, :) = 3 * a(1, 1, :) - 2; % r(1,1) should be + 1;
b(1, 2, :) = -17 * a(1, 2, :) + 8; % r(1,2) should be - 1;
% rest of r should be random between +1 and -1
% Compute correlations on third dimension
% Remove means
az = bsxfun(@minus, a, mean(a,3));
bz = bsxfun(@minus, b, mean(b,3));
% Standard Pearson correlation coefficient formula
a2 = az .^ 2;
b2 = bz .^ 2;
ab = az .* bz;
r = sum(ab, 3) ./ sqrt(sum(a2, 3) .* sum(b2, 3));

2 件のコメント

Niko
Niko 2011 年 9 月 15 日
Thank you so much David! Works like a charm and runs in no time.
André Gadêlha
André Gadêlha 2017 年 10 月 10 日
Dear David Young,
.
Why did you use this formulas to calculate the correlation:
.
b(1, 1, :) = 3 * a(1, 1, :) - 2; % r(1,1) should be + 1;
b(1, 2, :) = -17 * a(1, 2, :) + 8; % r(1,2) should be - 1;
.
and why did you removed means?
.
Best Regards!

サインインしてコメントする。

その他の回答 (1 件)

Mustapha Adamu
Mustapha Adamu 2018 年 12 月 10 日

1 投票

Dear David,
How do you go about this if you have nans,
Kind regards;
mustapha

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2011 年 9 月 14 日

回答済み:

2018 年 12 月 10 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by