Matlab

How Do I Create a for Loop in MATLAB?

A for loop is a useful tool in programming languages for repeating a section of code based on specific conditions. It continuously runs the code block as long as the predefined requirements are met. What sets the for loop apart from other loop types is its explicit loop counter, also called the loop variable. This loop variable helps keep track of the iteration order and allows the loop body to understand the sequence of each iteration.

This article will teach us how to create a for loop in MATLAB.

How Do I Create a for loop in MATLAB?

In MATLAB, a for loop serves as a structure for controlling repetition making it simple to design a loop that must be executed a predetermined number of times. The for-loop syntax in MATLAB is as follows:

for variable = startValue : increment : endValue
    % Put code here
end

 

To execute a for loop, the user needs to specify the startValue and endValue. Optionally, they can also define an increment to determine the step size by which the loop is incremented. If the increment is not explicitly defined, the loop will run at a default step size of 1.

Example 1

This is a simple MATLAB code that describes how to create a for loop in MATLAB.

for x = 1:10
   fprintf('The value of x: %d\n', x);
end

 

The above code will run 10 times, printing out the values from 1 to 10.

Output

Example 2

Let’s see another example:

for x = 1:2:10
   fprintf('The value of x: %d\n', x);
end

 

The above code prints out the values from 1 to 10 with an increment of 2.

Output

Example 3

In this example, for loop is used to decrement the values.

for x = 10:-1:1
   fprintf('The value of x: %d\n', x);
end

 

The code will print 10 values starting from 10 and ending at 1 because of step size -1.

Example 4

A nested loop is a type of loop that includes a for loop inside another for loop. The implementation of the nested for loop that generates a pattern in MATLAB is shown in the code below.

n=input('enter row number = ') %enter row number to generate pattern upto that row
for i=n+1:-1:2
  for j=i-1:-1:1
    fprintf('*');
  end
  fprintf('\n');
end

 

In the above code, the user has to enter a row number to generate patterns up to that row. After that, two for loops are used. The outer for loop will run from n+1 to 2 and the inner for loop will run from i to 1.

Within the inner loop, the fprintf(‘*’) statement is used to print an asterisk (*) character, representing a pattern element. This will be executed i-1 times in each iteration of the inner loop, creating a horizontal line of asterisks. After the inner loop completes, fprintf(‘\n’) is utilized for printing the new line character, which is used to move a cursor to the next line, creating a new row of the pattern. The pattern’s number of rows is controlled by the outer loop. It starts from n+1 and decrements by 1 in each iteration until it reaches 2, creating a decreasing pattern. After the execution of the code, it generates a pattern consisting of asterisks in descending order, with each row having one less asterisk than the previous row.

Conclusion

Making a loop that needs to run a particular number of times is simple by using a MATLAB for loop, which is a representation of a repetition control structure. This tutorial described the for loop in MATLAB using practical examples. Understanding the for loop syntax and examples will help you effectively use the for loop in various MATLAB codes.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.