필터 지우기
필터 지우기

nested for loops don't work

조회 수: 1 (최근 30일)
RD
RD 2017년 7월 18일
댓글: RD 2017년 7월 18일
I have a problem with this piece of code with nested for loops:
for k=1:6
matrix=zeros(3,3,6);
for i=1:3
for j=1:3
matrix(i,j,k)=i+j+k;
end
end
matrix(:,:,k)
end
matrix
Don't pay attentio to matrici(i,j,k)=i+j+k; It's just a way to fill the matrix. The problem is that: when I print matrix(:,:,k), they appears correctly. But when I print matrix at the end of code, only matrix(:,:,6) is correctly filled. The others are alla zeros. I can't understand where the mistake is. Thanks

채택된 답변

Andrei Bobrov
Andrei Bobrov 2017년 7월 18일
matrix=zeros(3,3,6);
for k=1:6
for i=1:3
for j=1:3
matrix(i,j,k)=i+j+k;
end
end
end
matrix
  댓글 수: 1
RD
RD 2017년 7월 18일
What a silly mistake I made!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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