Correlation with two matrices
20 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
David Hill
2022-12-12
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 个评论
Bora Eryilmaz
2022-12-12
编辑:Bora Eryilmaz
2022-12-12
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 个评论
Bora Eryilmaz
2022-12-14
编辑:Bora Eryilmaz
2022-12-14
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!