Borrar filtros
Borrar filtros

nested for loops don't work

1 visualización (últimos 30 días)
RD
RD el 18 de Jul. de 2017
Comentada: RD el 18 de Jul. de 2017
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

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 18 de Jul. de 2017
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 comentario
RD
RD el 18 de Jul. de 2017
What a silly mistake I made!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by