Backwards/Reverse in "Direct" For Loop

9 次查看(过去 30 天)
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?

回答(1 个)

James Tursa
James Tursa 2018-3-15
编辑:James Tursa 2018-3-15
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 个评论
Mark Tenorio
Mark Tenorio 2018-3-15
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.

请先登录,再进行评论。

类别

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