Calculating correlations across multiple time series

조회 수: 14 (최근 30일)
Jlil
Jlil 2023년 2월 9일
댓글: Jlil 2023년 2월 12일
I have two matrices with data consisting from 324 trials, and 102 channels as follows:
alphapower [324×102 double]
betapower [324×102 double]
I would like to calculate a matrix of correlation and beta (linear) across all channels. So as an output i need a matrix which is [102 x 102 double] which holds the correlations across all channels. The following code i believe achieves this:
% These two variables holds the original data
% which is here replaced by random numbers
alphapower = randn(324,102);
betapower = randn(324,102);
% Instantiation and loop across both dimensions - must be a better way of
% doing this
betas = zeros(102,102);
rsquare = zeros(102,102);
whichstats = {'beta','rsquare'};
for i=1:102
for j=1:102
stats = regstats(alphapower(:,i),betapower(:,j),'linear',whichstats);
betas(i,j) = stats.beta(2);
rsquare(i,j) = stats.rsquare;
end
end
% Lets visualize our results
figure
heatmap(betas)
figure
heatmap(rsquare)
Does anyone know a better way to write this maybe using vectorization? The code above is surprisingly fast but i need to do this operation many times. I am also frustrated i havent found a nicer way of solving this.

채택된 답변

Sarvesh Kale
Sarvesh Kale 2023년 2월 10일
Hi Jlil,
If you are trying to compute the correlation between columns of a matrix or between columns of two different matrices then you should take a look at corr and xcorr function inbuilt in MATLAB https://in.mathworks.com/help/stats/corr.html
following code should do the trick
x=randn(324,102);
y=randn(324,102);
Rxy=corr(x,y); % replace the inputs with your matrices
figure;
title('correlation Rxy')
heatmap(Rxy) % show Rxy
I hope this answers your query, please accept the answer if it does.
Thank you.
  댓글 수: 1
Jlil
Jlil 2023년 2월 12일
Hi Sarvesh that got me on the right track - thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by