MATLAB Answers

Correlation Coefficient of two 3-D matrices

2 views (last 30 days)
I have two matrices, lon x lat x time [288 142 21]. How can I find the correlation coefficient at each grid point and plot the results?

Accepted Answer

Daniele Mascali
Daniele Mascali on 18 Mar 2021
Considering A and B your two matrices, you could use the following code:
%reshape each matrix
SIZE = size(A);
A_2d = reshape(A,[SIZE(1)*SIZE(2),SIZE(3)]);
B_2d = reshape(B,[SIZE(1)*SIZE(2),SIZE(3)]);
% Calculate Pearson's correlation. It can be easily computed by transoforming data
% to zero mean and unit standard deviation (i.e., zscore). Then it is just a dot product.
A_2d_z = zscore(A_2d');
B_2d_z = zscore(B_2d');
correlation_1d = sum(A_2d_z.*B_2d_z)/(SIZE(3)-1);
%reshape back to the original size
correlation_2d = reshape(correlation_1d,[SIZE(1),SIZE(2)]);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by