Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

arrays - How can I get all pairwise correlations of a list of 2d matrices in MATLAB?

EDIT: The question has been edited after accepting the answer, to make it (hopefully) clearer.

Given the 3d matrix M(m, n, k), how do I calculate the 2d correlation matrix M(k, k) whose (i, j) entry is corr(M(m, n, i), M(m, n, j)).

Layman's terms

For example, I have a 3d matrix M(20, 20, 100), and I need a 2d matrix M(100, 100) which is a correlation matrix of each pairwise combination of M(20, 20, i), where i = 100. Since M(100, 100) is a correlation matrix, each cell is a single correlation coefficient (r), and the matrix is symmetric:

     a     b     c    ...
a    1     r_ab  r_ac 
b    r_ba  1     r_bc
c    r_ca  r_cb  1
...

Matlab MWE

I tried combinations of loops, corrcoef, corr2, with no avail.

% 3d matrix
m = rand(20, 20, 100);

% wrong output
r = corrcoef(m(:, :));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You only need to reshape m so that each matrix is linearized into a column. Then corrcoef gives the desired result:

r = corrcoef(reshape(m, [], size(m,3)));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...