creating a nested loop to change values within a for loop

2 ビュー (過去 30 日間)
Native
Native 2019 年 2 月 19 日
コメント済み: Stephen23 2019 年 2 月 19 日
Hi,
here is a code segment I am working on
n = 132 ;
M = cell(n,1);
for n = 1:132
for i = 1:132
for j = 1:49
if j~=2
[R P] = corrcoef(young_50(n,:,2),young_50(i,:,j))
r(j,i) = R(1,2);
p(j,i) = P(1,2);
end
end
end
M{n} = r;
M{n}(2,:) = []
end
what I need to do is change "if j~=2" to 3, 4,5...until 49, and also change young_50(n,:,2) similiarly. I then need to save M as M3, M4... to represent that particular output.
How can I go about doing that?
  1 件のコメント
Stephen23
Stephen23 2019 年 2 月 19 日
" I then need to save M as M3, M4... to represent that particular output."
Using numbered variables is a sign that you are doing something wrong in your code. You should use indexing instead.

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

回答 (2 件)

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019 年 2 月 19 日
Hi,
I think something like this should help. I am not too sure if I understood your question right, please let me know if you wanted something else.
n = 132 ;
M = cell(n,42);
for n = 1:132
for z = 2:49
for i = 1:132
for j = 1:49
if j~=z
[R P] = corrcoef(young_50(n,:,z),young_50(i,:,j))
r(j,i) = R(1,z);
p(j,i) = P(1,z);
end
end
end
M(n,z) = r;
M(n,z)(2,:) = [] % not sure what you are trying to do with this but the syntax is incorrect
end
end
Regards

Andrei Bobrov
Andrei Bobrov 2019 年 2 月 19 日
[ii,k,jj] = size(young_50);
yy = permute(young_50,[2,3,1]);
y1 = reshape(yy(:,[1,3:end],:),k,[]);
b = squeeze(yy(:,2,:));
p = corr(y1,b);
M_array3D = reshape(p,jj-1,[],ii);

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by