Calculating correlations across multiple time series

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.

 채택된 답변

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 2월 9일

댓글:

2023년 2월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by