A Complete Guide on Loops in Matlab With Relevant Examples

There are several situations when one needs to execute a coding program several times. These statements are sequentially executed, which means it will first execute the first statement, then second, and so on. Programming languages offer several control structures that grant to execute complicated programs. With the help of a loop statement, one can execute the statements or a group of statements several times. The loops in MATLAB can be understood through the given flow diagram:

Loop Architecture

Matlab offers the following kinds of loops that handle the requirement of the looping a statement. Let’s check all these loops in Matlab:

Loops in MATLAB

“While” loops in Matlab

It is used to repeat the number of statements or a statement when the given condition is true. It always checks the condition of the loop body before executing it.

Syntax:

while <expression>

   <statements>

end

When an expression is taken as true, then the result of it is nonempty, and it has all nonzero components that might be real or logical numeric. Otherwise, the expression is considered to be false.

Example:

Input:

b = 1;

while ( b < 10 )

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

b = b+1;

End

Output:

Value of b: 1

Value of b: 2

Value of b: 3

Value of b: 4

Value of b: 5

Value of b: 6

Value of b: 7

Value of b: 8

Value of b: 9

“For” loops in Matlab

It is used to execute the sequential statement a number of specific times, and it abbreviates the program, which is used to manage the loop variable.

Syntax:

for index = values

   <program statements>

            …

end

Here, the values can be of any one kind among these three values that are listed below:

initval:endval:

This value function can be used to increase the index variable that is incremented by 1 from initval to endval, and this will repeat the execution of the code statements until the index has greater value than endval.

Example:

Input:

for b = 1:10 

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

end

Output:

value of b: 1

value of b: 2

value of b: 3

value of b: 4

value of b: 5

value of b: 6

value of b: 7

value of b: 8

value of b: 9

value of b: 10

initval:step:endval

This is used to increment the index function by the step value for each iteration, and it will decrement the value if the step is negative.

Example:

Input:

for b = 2.0: -0.1: 1.0

   disp(b)

end

Output:

2

1.90000

1.80000

1.70000

1.60000

1.50000

1.40000

1.30000

1.20000

1.10000

1

valArray

It is used to create an index column vector from a particular array, for instance: on the initial iteration, index = valArray (:, 1). The loop will execute for the n times, where n is considered to be the number of columns for valArray, which is given by numel(valArray, 1, :). The input valArray involves a cell, string, struct, or array.

Example:

Input:

for b = [12,10,15,22,27]

   disp(b)

end

Output:

12

10

15

22

27

“Nested” loops in Matlab

It is used to implement a single loop or more than one loop within other loops in Matlab. This can be done for ”while” loop or “for” loop statements.

Syntax:

“For” loop

for m = 1:j

   for n = 1:k

      <statements>;

   end

End

“While” loop

while <expression1>

   while <expression2>

      <statements>

   end

End

Example:

Input:

for a = 2:30

   for b = 2:30

      if(~mod(a.b)) 

         break; % if factor found, not prime

      end 

   end

   if(b > (a/b))

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

   end

End

Output:

2 is prime

3 is prime

5 is prime

7 is prime

11 is prime

13 is prime

17 is prime

19 is prime

23 is prime

29 is prime

Loop control statements in Matlab

It is used to change the execution of the normal sequences. Whenever an execution leaves a loop, the elements of the objects will destroy the scope of that particular object. There are two different types of control statements in Matlab:

Break statement

It is used to terminate the execution of a while or for loops in Matlab. The statements that are defined after the break statement will not get executed. Whereas, in the nested loops, it exists from a specific loop in which it has occurred. The control of the statement is passed to the end of the given loop.

Flow diagram

MATLAB break statement

Example:

Input:

b = 1;

while (b < 10 )

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

   b = b + 1;

      if( b > 5)

         break;

      end 

end

Output:

value of b: 1

value of b: 2

value of b: 3

value of b: 4

value of b: 5

Continue statement

This statement can pass the control to the upcoming or next iteration in a while or for loops in Matlab. Somehow, it also works as a break statement. Rather than forcing the termination from the loop, it moves to the next iteration of the given loop, and it skips any of the codes in between the program.

Flow diagram:

MATLAB continue statement

Example:

Input:

b = 1;

while b < 11

   b = b + 1; 

   if b == 5

      continue;

   end 

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

end

Output:

value of b 1

value of b: 2

value of b: 3

value of b: 4

value of b: 6

value of b: 7

value of b: 8

value of b: 9

value of b: 10

Conclusion

Matlab grants the user to use the various kinds of loops in Matlab programming that are used to handle different looping requirements that involve: while loops, for loops, and nested loops. Besides these, it also has two different control statements that are: break statement and continue statement, which is used to control the looping of the statement in a program. Using the loops for specific repetitions statements can be a great way to shorten the final coded program.

If you have any difficulties related to assignments of Matlab, then you can avail of services for Matlab homework help and help with MATLAB homework. We have experts’ team that can be accessible 24/7 with high-quality data. The services are offered at minimal prices, and we can provide you instant help that will deliver the assignments before the deadlines. So, avail of our services, and get relaxed from the complicated Matlab assignments.

Related Posts
Handy Tips on How to Write an Assignment From Scratch

How to write an assignment has become a prevalent question among students as writing an assignment is an essential part Read more

Top 8 Types of Python Frameworks You Should Learn

Python is the most popular programming language. Python Frameworks is the collection of certain packages and modules for writing web Read more

Leave a Comment