# For loop execution problem in matlab

1 visualización (últimos 30 días)
Aftab Ahmed Khan el 12 de Jun. de 2014
Comentada: José-Luis el 12 de Jun. de 2014
Hi everyone, i want to do this, like for i=1:10, this function changes 0 to 1 on every first 10 loop iteration which is working in this program, but when the 11 iteration executes then the first 1 in the array changes back to 0 and when the 20th iteration executes i again get all the values equal to 0. Thank you.
clear all;
clc;
A = zeros(1,10);
for i = 1:20
if i<=length(A)
A(i) = 1;
elseif i>length(A) %%%this statement doesn't work
A(i) = 0;
end
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

José-Luis el 12 de Jun. de 2014
Editada: José-Luis el 12 de Jun. de 2014
A = zeros(1,10);
for ii = 1:20
if i<=length(A)
A(ii) = 1;
elseif i>length(A)
A(ii) = 0;
A(ii - 10) = 0;
end
end
##### 2 comentariosMostrar NingunoOcultar Ninguno
Aftab Ahmed Khan el 12 de Jun. de 2014
Hi Jose, Thanks. Just to let you know that this is also a part of my yesterday question which i managed to solve somehow, thanks to you guys as well.
José-Luis el 12 de Jun. de 2014
My pleasure.

Iniciar sesión para comentar.

### Categorías

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

### Community Treasure Hunt

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

Start Hunting!

Translated by