Loops in MATLAB

Learn via video courses
Topics Covered

Matlab is a programming language that is widely used in the fields of engineering, science, and mathematics for its high-performance numerical computing properties. One of the key features of Matlab is to perform repetitive tasks using a loop, which is a control structure that allows the execution of a set of instructions repeatedly until a specific condition is met. Let us explore different types of loops in matlab in this article.

end Keyword in MATLAB

The end keyword in Matlab is used to specify the termination of various statements in matlab. Various uses of the end keyword in matlab include:

  • Mark the termination of loops, such as 'for' and 'while,' ensuring the execution of the specified code block until the loop condition is satisfied.
  • In array indexing, 'end' represents the last element of an array. For example, array(1:end) refers to all elements from the first to the last in the array.
  • Termination switch statement, if conditions and scope of the try section. It should also be used to terminate the parallel loop in 'parfor' statements.
  • The end statement can also be optionally used for closing a function declaration. It is usually used to improve the readability of code, but if a function in a file is terminated with an end statement, then all functions in the file should be terminated with an end statement.

We can extract the last three elements of the array using the end keyword as follows:

You can explore more examples of the end keyword in the upcoming section.

Different Types of MATLAB Loops

Matlab supports various types of loops, enabling programmers to perform iteration and solve various computational problems. The two primary loop constructs are the 'while' loop and the 'for' loop.

While Loop

The 'while' loop in MATLAB is a control flow structure that allows a set of statements to be executed repeatedly as long as a specified condition remains true. The syntax of the while loop in matlab is:

Let us consider the example of finding first n prime numbers.

Explanation:

In this example, the loop iterates as long as the count of prime numbers found are less than the desired count 'n'. Inside the loop, the isprime function checks if the current number 'num' is prime. If true, the number is displayed, and the count of prime numbers is incremented. This process continues until the specified count 'n' is reached.

Let us modify the above example by adding a condition to restrict our search for prime numbers within a limit.

Explanation:

In this example, the loop continues as long as the first n prime numbers are found or the break condition is met. The isprime() pre-defined function is used to check if a number is prime and there is a condition to break out of the loop if 'num' exceeds the predefined limit. This ensures the loop terminates when the specified condition is met, allowing for flexibility in controlling the loop execution.

For Loop

The 'for' loop in MATLAB is a control flow statement used for iterating over a sequence of values or elements. It is particularly useful when the number of iterations is predefined. The syntax of the for loop in matlab is:

Let us consider a simple example of calculating the sum of the root of the first n numbers.

Explanation:

In this example, the loop iterates over the values from 1 to 'n', and for each iteration, the square root of the current number 'i' is calculated using the sqrt function. The individual results are added using a variable. Finally, the total sum is displayed using the disp function.

Let us modify the above example by including a condition to exclude multiples of 5 from the final sum.

Explanation:

In this example, the loop iterates over the values from 1 to 'n', and within the loop, an 'if' statement checks if the current number 'i' is a multiple of 5 using the mod function. If true, the 'continue' statement skips the rest of the loop for that iteration.

Conclusion

  • MATLAB is a powerful programming language that is highly used in numerical computing with an extensive library of mathematical functions and sophisticated data visualization tools. A loop in matlab is a control flow structure used for the repetition of a set of instructions based on a specified condition
  • The 'end' keyword in MATLAB is a terminator for various statements such as for, while, switch, try, and parfar. It can also be optionally used to close a function declaration and is also used in array indexing, representing the last element of an array.
  • The 'while' loop in matlab allows for the repeated execution of a set of code as long as a specified condition remains true. This loop structure is particularly useful when the number of iterations is uncertain or dynamically determined during program execution.
  • The 'for' loop in MATLAB is a control structure used for iterating over a specified range of values. This is highly used when the number of iterations is predetermined, and the flow of control can also be modified using break and continue statements.