Calculating correlations across multiple time series

11 views (last 30 days)
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.

Accepted Answer

Sarvesh Kale
Sarvesh Kale on 10 Feb 2023
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.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!