Backwards/Reverse in "Direct" For Loop

7 visualizaciones (últimos 30 días)
Mark Tenorio
Mark Tenorio el 15 de Mzo. de 2018
Comentada: Mark Tenorio el 15 de Mzo. de 2018
Is there a way to back backwards in a "direct" for loop?
If I had the vector A = [1 4 7 8]
How would I make it where:
for i = 1:A
increments backwards starting from 8?

Respuestas (1)

James Tursa
James Tursa el 15 de Mzo. de 2018
Editada: James Tursa el 15 de Mzo. de 2018
for k=numel(A):-1:1
k would have the values 4, 3, 2, 1 in turn. So indexing using A(k) you would be accessing A(4), A(3), A(2), and A(1) in turn.
  2 comentarios
Mark Tenorio
Mark Tenorio el 15 de Mzo. de 2018
I will try this and see if this works.
I just want my for loop to go from 8, 7,4, 1 when I am using k as a variable.
Mark Tenorio
Mark Tenorio el 15 de Mzo. de 2018
Nevermind. I can just use fliplr.

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