MATLAB Answers

How do I create a for loop in MATLAB?

조회 수: 12,717(최근 30일)
pedro marin
pedro marin 2012년 3월 5일
댓글: Walter Roberson 2021년 9월 3일
I am completely lost in for loops, I just don't get it. The book and my professor haven't helped much. Where can I get help?
  댓글 수: 2

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

채택된 답변

Walter Roberson
Walter Roberson 2012년 3월 5일
편집: MathWorks Support Team 2018년 11월 9일
A basic for loop in MATLAB is often used to assign to or access array elements iteratively. For example, let’s say you have a vector A, and you want to simply display each value one at a time:
A = [3 6 9 4 1];
for i = 1:length(A)
For more examples using for loops, see:
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 9월 3일
Not sure why it is said that it doesn't work?
A = [3 6 9 4 1];
for i = 1:length(A)
3 6 9 4 1

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

추가 답변(5개)

Jan 2012년 3월 5일
You can get help from the documentation of Matlab:
doc for
help for
There you find examples and explanations.
  댓글 수: 1
Meenakshi Bhardwaj
Meenakshi Bhardwaj 2018년 7월 19일
Thanks so much. Finally, I understood what is for loop.

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

Jan Afridi
Jan Afridi 2017년 9월 29일
For loop repeat itself for a given number of input. The syntax for “For Loop Matlab” is
for variable = expression
Program Statement
In the above syntax, the expression has one of the following forms.
Initial value : Final value
for x = 1:10
fprintf('value of x: %d\n', x);
Initial value : Step : Final value
for x = 1:2:10
fprintf('value of x: %d\n', x);
Value Array
for x = [1 4 6 8 90]
To learn more about in detail visit this link For Loop Matlab

Annesha Ahsan
Annesha Ahsan 2019년 1월 4일
for i=1:2:10
UnablEto get the answer

mohamed mohamed
mohamed mohamed 2021년 2월 6일
편집: Walter Roberson 2021년 7월 31일
for x = 1:10
fprintf('value of x: %d\n', x);
value of x: 1 value of x: 2 value of x: 3 value of x: 4 value of x: 5 value of x: 6 value of x: 7 value of x: 8 value of x: 9 value of x: 10

Narasimman P
Narasimman P 2021년 7월 30일
for a=1:10

Community Treasure Hunt

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

Start Hunting!

Translated by