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 Center および File ExchangeCorrelation and Convolution についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by