creating a nested loop to change values within a for loop

2 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 19 Feb 2019
" 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.

Sign in to comment.

Answers (2)

Yasasvi Harish Kumar
Yasasvi Harish Kumar on 19 Feb 2019
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 on 19 Feb 2019
[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);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!