Backwards/Reverse in "Direct" For Loop

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일

1 개 추천

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.

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

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2018년 3월 15일

댓글:

2018년 3월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by