creating a nested loop to change values within a for loop

1 次查看(过去 30 天)
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by