Correlation with two matrices
20 views (last 30 days)
Show older comments
Hi everyone,
I would like to find the correlation about two matrices the same dimensions. I have two matrices with 64 channles and I need to find the correlation between the first value of first matrix and the first, second, third.... until sixty-four value of second matrix and viceversa.
How can I do this?
Thanks a lot!
0 Comments
Answers (2)
David Hill
on 12 Dec 2022
You could just manually do it yourself.
A=randi(100,10);B=randi(100,10);
M=mean(A);N=mean(B);
r=zeros(size(A,2),size(B,2));
for m=1:size(A,2)
for n=1:size(B,2)
r(m,n)=sum((A(:,m)-M(m)).*(B(:,n)-N(n)))/sqrt(sum((A(:,m)-M(m)).^2.*(B(:,n)-N(n)).^2));
end
end
r
0 Comments
Bora Eryilmaz
on 12 Dec 2022
Edited: Bora Eryilmaz
on 12 Dec 2022
You can compute the pairwise correlation between columns of matrices as follows:
% Data matrics with 64 channels (columns)
A = rand(10,64);
B = rand(10,64);
% Vector of pairwise correlation values
C = corr(A,B); % All pairs of columns
C = diag(C) % Matching pairs of columns
3 Comments
Bora Eryilmaz
on 14 Dec 2022
Edited: Bora Eryilmaz
on 14 Dec 2022
Your code seems unnecessarily complicated. If you already have the matrices L and R, the correlations variable need not be a cell array. Something like this should work:
% Data matrics with 64 channels (columns)
L = rand(100,64);
R = rand(100,64);
% Vector of pairwise correlation values
C = corr(L,R); % All pairs of columns
correlations = diag(C) % Matching pairs of columns
The second loop that goes over the columns of L and R matrices is not needed since the corr() command already handles that for you, given the whole matrices. Unless of course if your L and R "matrices" have a more complicated structure, like being cell arrays, etc.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!