Correlation using for loop

17 ビュー (過去 30 日間)
Elizabeth Yeap
Elizabeth Yeap 2021 年 1 月 11 日
コメント済み: Elizabeth Yeap 2021 年 1 月 12 日
Dear all,
I have 2 matrices, M_ET [3700 12] and M_Compiled [7400 12]. Using a for loop, I want to do the following:
  1. Correlate [100 12] of M_ET with [100 12] M_Compiled to get [1 12] of values stored in C_ET.
  2. Repeat 1. for the remaining rows of M_ET and M_Compiled, where rows increase by 100.
C_ET(1,:) = diag(corr(M_ET(1:100,:), M_Compiled(1:100,:)));
C_ET(2,:) = diag(corr(M_ET(101:200,:), M_Compiled(101:200,:)));
C_ET(3,:) = diag(corr(M_ET(201:300,:), M_Compiled(201:300,:)));
My C_ET should therefore be [74 12]. However, my C_ET is instead giving me [1 12]. Any help is greatly appreciated. Thank you.
% M_ET = [3700 12]
% M_Compiled = [7400 12]
% C_ET = [37 12]
for row = 1:100:7400
for r = 1:100:3700
row1 = row;
row2 = row + 99;
r1 = r;
r2 = r1 + 99;
C_ET(:,:) = diag(corr(M_ET(r1:r2,:), M_Compiled(row1:row2,:)));
end
end
  4 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 1 月 11 日
If you describe the question with a sample example, it will be easier to answer.
  • What do you have?
  • What are you trying to do?
Elizabeth Yeap
Elizabeth Yeap 2021 年 1 月 11 日
Hi @KALYAN ACHARJYA, I have tried to provide further explanations.

サインインしてコメントする。

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 1 月 11 日
Problem, you are trying to access the rows to 7400, but the rows in M _ET [3700 12] have a maximum of 3700, as
M_ET [3700 12]
M_Compiled [7400 12]
If you manage to provide enough rows to the M _ET data matrix, there will be no problem
C_ET=cell(1,74);
l=1;
for i=1:100:7400-99
C_ET{l}=diag(corr(M_ET(i:i+99,:), M_Compiled(i:i+99,:)));
l=l+1;
end
C_ET=cell2mat(C_ET);
Verify
>> whos C_ET
Name Size Bytes Class Attributes
C_ET 12x74 7104 double
  1 件のコメント
Elizabeth Yeap
Elizabeth Yeap 2021 年 1 月 12 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by