creating a nested loop to change values within a for loop
조회 수: 2 (최근 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
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
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
댓글 수: 0
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);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!