Backwards/Reverse in "Direct" For Loop

조회 수: 11 (최근 30일)
Mark Tenorio
Mark Tenorio 2018년 3월 15일
댓글: Mark Tenorio 2018년 3월 15일
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.
Mark Tenorio
Mark Tenorio 2018년 3월 15일
Nevermind. I can just use fliplr.

댓글을 달려면 로그인하십시오.

카테고리

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