Borrar filtros
Borrar filtros

Using i as an index within a loop in Matlab 2016a

1 visualización (últimos 30 días)
Ken
Ken el 5 de Oct. de 2016
Comentada: Ken el 5 de Oct. de 2016
Is there a simple, global way to force Matlab 2016 to allow i to be used as a vector index within a loop statement that uses i as the loop index? I've been using i as a convenient index in for loops forever, and the simple assignment statement in the for statement was always sufficient to convert it from it's built in complex type to an integer. i.e.
for i=1:5,
y(i)=stuff;
end;
In Matlab 2016a, this code barfs because i retains its complex definition within the scope of the loop, so can't be used as the index into a vector within the loop. Adding an explicit assignment to an integer e.g. i=0, before the loop, or changing i to another variable name works, but I'd rather not modify every routine that uses i as an index within a loop.
  1 comentario
Steven Lord
Steven Lord el 5 de Oct. de 2016
Please show the full, exact text of the error message you receive. The behavior you're describing shouldn't happen and doesn't happen when I use release R2016a or R2016b.
for i = 1:5
y(i) = 23;
end

Iniciar sesión para comentar.

Respuesta aceptada

Matthew Eicholtz
Matthew Eicholtz el 5 de Oct. de 2016
I don't know what you mean by "this code barfs". I ran a simple for-loop in MATLAB R2016a with i as the index variable and experienced no barfing.
But, as an aside, because of the complex definition of i, I tend to prefer using ii instead of i in for-loops.
  1 comentario
Ken
Ken el 5 de Oct. de 2016
My mistake, sorry to waste your time. This issue was reported by a customer using our code, and I mis-interpreted the symptom working over the phone. Turns out the problem was another difference in later Matlab versions that we were already aware of. Thanks for the thoughtful reply.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical 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