Loop in MATLAB : The Best Ever Guide With Examples

Most of the students are not well aware of the loops in MATLAB. Likewise, other programming languages MATLAB also has some loop. So, before we are getting into the loop in MATLAB, let’s understand what exactly the loop does in programming. With the help of the loop, you can execute the block of the statements many times. Thus the statement executed sequentially i.e., the first statement executed, the second one, and so on.

Here in this diagram, you can see the general form of loop statements in the programming languages. In MATLAB, you find the various types of the loop for different purposes. All these loops can fulfill the requirements of while loops, for loop and the nested loops. You can also declare your loop in MATLAB. But you have to make sure that the loop should be written as script. You should not write it directly in the command window. You can create the new loop in MATLAB with the help of the toolbox in MATLAB.

While Loop

The while loop works on the simple phenomena. It executes the statement continuously until the specific condition becomes false. Here is the syntax of while loop:-

While <expression>

<statement>

End

In the while loop, the program statement repeatedly executes as long as the expression becomes false. In other words, the result contains the non zero elements or non-empty the condition becomes true, otherwise false.

Example

x = 20; 

% while loop execution while( a < 25 )

 fprintf(‘value of x= %d\n’, a); 

x = x + 1; 

end 

When the code above is executed, the result will be:

Value of x=21

Value of x=22

Value of x=23

Value of x=24

Value of x=25

For loop

The for loop is the best loop in any programming language. In the for loop, you get a repetition control structure. It allows you to have full control to write a loop that needs to execute any number of times. The syntax of a for loop in MATLAB

for index = values 

<programming statements>

… 

end

Values: There can be following forms of value: 

initval:endval — Here, you need to specify the index variable from initval to endval by 1.

And the statement executes until the index is greater than endval.

Initval:step:endval — In this, the index increments by the value step on each iteration. And it decrements when the step is negative. 

valArray — In this, the program creates a column vector index from subsequent columns of the array. valArray on each iteration. 

Example

for a = 10:15 

fprintf(‘value of a: %d\n’, a); 

end

When the code above is executed, the result will be:

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

for a = 1.0: -0.1: 0.0 

disp(a) 

end 

When the code above is executed, the result is: 

1

9/10 

4/5 

7/10 

3/5 

1/2 

2/5 

3/10 

1/5 

1/10 

0

for a = [10,14,19,6,15] 

disp(a) 

end 

When the code above is executed, the result will be: 

10

14

19

6

15

The Nested Loops 

You can also use a loop inside another loop in Matlab. There are two types of nested loops in MATLAB. The first one is nested for loop, and the other one is nested while loop.

Here is the syntax of for loop in MATLAB

for m = 1:

j for n = 1:

k ; end 

end 

The syntax for a nested while loop statement in MATLAB is as follows:

while <expression>

while <expression2>

<statement>

end

 end

Example

for i=2:20 

for j=2:20 

if(~mod(i,j)) 

break; % if factor found, not prime 

end 

end 

if(j > (i/j))

fprintf(‘%d is prime\n’, i);

end 

end 

When the code above is executed, the result is:

2 is prime 

3 is prime 

5 is prime 

7 is prime 

11 is prime 

13 is prime

17 is prime  

Loop Control Statements 

The Loop control statement is used to make the change execution from the normal sequence of the loop. In this, all the automatically created objects in the scope destroyed when the scope leaves by the execution. Hereby the scope means where the variable is valid in MATLAB.

For example, the scope of the variable remains in the loop from the beginning of the conditional code to the end of the code. The scope also tells MATLAB what to do when the conditional becomes false. Here in MATLAB, you find two types of loop control statements i.e., the break statement and the continue statement. 

Break statement

With the help of the break statement, we can terminate the execution of for or while loops. If we put the break statement in any loop, then the statement which appears after the break statement is not executed. On the other hand, if we talk about the nested loops, the break only works in the loop in which it occurs. Then the statement automatically passes to the next loop.

Example 

a = 5; 

% while loop execution 

while (a < 10 ) 

fprintf(‘value of a: %d\n’, a); 

a = a+1; 

if( a > 10) 

% terminate the loop using break statement

 break; 

end 

end

When the code above is executed, the result is:

value of a: 5

value of a: 6 

value of a: 7 

value of a: 8

value of a: 9

value of a: 10

Continue Statement 

In the continue statement, the control automatically passes the next iteration for both the for loop and the while loop. Most of the time, the continue statements work like the break statement in MATLAB. The continue statement puts force on the next iteration to take place instead of forcing the transmission. 

Example 

a = 5; 

%while loop execution 

while a < 10 

if a == 15 % 

skip the iteration 

a = a + 1; 

continue; 

end 

fprintf(‘value of a: %d\n’, a); 

a = a + 1; 

end

When the code above got executed, the result is:

value of a: 5

value of a: 6

value of a: 7

value of a: 8 

value of a: 9 

Conclusion

At last, we have seen all the loop in MATLAB. Now you may be quite confident about the loop in MATLAB. You can use these examples or other examples to learn these loop in MATLAB. So what are you waiting for? Start implementing these loop in your MATLAB program now?

If you are looking for the best matlab assignment help, then we are here to provide you the best matlab assignment help online. Grab the best deal on best matlab assignment help and help with matlab assignment.

Leave a comment