Correlation with two matrices

20 views (last 30 days)
Felicia DE CAPUA
Felicia DE CAPUA on 12 Dec 2022
Commented: Felicia DE CAPUA on 14 Dec 2022
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!

Answers (2)

David Hill
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
r = 10×10
-0.7325 -1.5279 0.5441 -1.1353 1.3671 -1.9351 -1.4018 0.8595 0.3189 0.8992 0.0661 0.1537 0.7952 0.6901 -0.5238 0.0602 -1.4856 1.2302 -0.9858 0.1995 -1.1532 -2.2351 1.1573 1.5280 -0.2541 -0.0752 0.8163 0.5013 -0.5230 -0.5775 0.1300 -0.4017 -0.7430 -0.1586 0.9928 -1.7321 0.4399 -0.2708 -0.7359 -0.3293 -0.8045 -1.8755 1.9652 2.4100 -0.1757 -0.6482 -0.6868 1.0841 0.0492 -1.6882 -0.1862 0.4648 -0.0476 -0.4808 0.2425 -0.5745 -0.6061 1.5960 0.0200 0.6626 -0.8411 -0.0155 0.0680 -0.9309 1.0928 0.8641 -0.5821 0.6830 0.5620 -0.2508 -0.4532 -0.6661 1.2019 1.5538 -0.9566 0.6813 1.0478 -0.1999 1.4598 -1.1943 -0.0501 -0.1554 -0.2710 -0.9457 -0.5791 0.9445 0.1837 0.7055 -0.0926 1.7323 -2.1825 -0.8110 2.0911 1.4211 0.6435 -1.3528 -0.0415 0.1698 2.3680 -1.8121

Bora Eryilmaz
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
C = 64×1
0.1045 0.2102 -0.1445 -0.0380 0.3045 -0.2592 0.2869 -0.5510 0.1050 0.0790
  3 Comments
Bora Eryilmaz
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
corerlations = 64×1
0.1035 -0.0462 -0.0224 -0.0031 -0.0396 -0.1127 0.0125 -0.0144 0.0199 -0.1041
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.

Sign in to comment.

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!