How to Use For Loop in MATLAB With Examples

Matlab has a number of functions that help the programmer to perform a certain task in an easier way. In this post, you will study a useful element of the Matlab programming i.e., For loop. It is a conditional iterative statement that is used in the coding language.

For loop in Matlab, check the applied condition and then implement the function as per the given statement that can be repeated several times. This will continue the work until it does not meet the desired condition. For loop also referred to as the loop variable because it allows the loop statement to know the sequence of each iteration. 

What is for loop in Matlab

The for loop in Matlab grants the programmers to repeat the certain commands. Therefore, if you want to repeat a few actions in a predefined manner, one can use this loop. There are several loop syntax in Matlab that is starting with the keyword like while or for and end with the statement ‘end’. The for loop statement is coded around a few sets of statements; therefore, it becomes necessary to tell the Matlab function that where to initiate and where to stop the execution. 

Syntax of For loop in Matlab:

For index = value

Statement

End

Now let’s discuss the details of ‘for loop’ in more detail:

Some of the examples of For loop in Matlab

For index = it involves multiple or single statements, values, and end.

This function is used to execute a defined set of statements that can be run several times, which specifies the conditions. The values can be written in the number of forms such as:

  • firstVal: lastVal: It is used to increment the index value by 1 from firstval to lastval; it can execute the set of statements until firstVal is higher than the lastVal.
  • firstVal: step: lastVal: It gradually increment the index value by defining “step” value, or it can decrement the value by “step” for negative values.
  •  valArray: to execute each iteration, it will create a column vector for valArray from the column of the given array.

Now take some examples of ‘for loop in Matlab’:

Decrement values

In this, the programmer can decrement the values of the defined interval.

Program: 

For a = 3.0 : -1.0 : 0.0

disp(a)

End

This program will execute the value by decrementing by ‘1.0’

The output will be:

3

2

1

0

Increment Values

It will increase the given values by the given number of intervals.

program:

for a = 5.0 : 2.0 : 12.0

disp(a)

end

the output will be incremented by 2:

5

7

9

11

Another example of it is: 

program:

for a = 2.0 : 1.0 : 5.0

disp(a)

End

Now, the output will be increment by the value 1:

2

3

4

5

Specified Values

It will run the statements for specified values

program:

for a = [2 3 5  7] disp(a)

end

This is how the output will represent:

Output:

2

3

5

7

Use of Repeat Statement for every Matrix Column

Here the A is a 3X3 Identity matrix

Program:

for A = eye (3,3)

disp(‘Current value:’)

disp(A)

end

Output:

Current value:

Diagonal matrix: 

1

0

0

Current value:

0

1

0

Current value:

0

0

1

Use of BREAK Statement

To exit from the ‘for loop in Matlab’,  the programmers can use the break statement. Without using the break statement, the following example will print the ‘END’ value after each iteration

Program:

for A = eye (2)

disp(‘Value:’)

disp(A)

disp(‘END’)

end

Output:

Value: 

Diagonal matrix:

1

0

End

0

End

If the programmer uses it with a break statement, then it will break the ‘for loop’ after the initial iteration.

Program:

for A = eye (3)

disp(‘Value:’)

disp(A)

break

disp(‘END’)

end

Output:

Value:

Diagonal matrix:

1

0

0

This shows that the statement is the break after the initial statement.

Conclusion

The ‘for loop’ can be used for repeating certain actions, or we can say that for loop is necessary to run a certain program multiple times. But you want to exit from a program; then, the programmer can use the break statement. If you want to jump the upcoming instructions and start with the next iteration, then you can use the continue statement. A single column vector can be iterate by using the transpose statement to generate a row vector. 

Still, you are not able to understand the use of for loop in Matlab; then, you can take our experts’ help who can provide you instant help with your MATLAB online help and MATLAB help online. We are also here to provide you the best online MATLAB help

Leave a comment