# For Loop in MATLAB

Posted on at

MATLAB is a well renowned technical computing software. It is widely used by almost every genere of science students. It has a great potential to simulate and has a very sound ability to compute what ever is required by the user.

Today I want to discuss the control statements in MATLAB. There are a number of them. The most important ones are as follows;

For statement

While statement

If statement

Switch case statement

Try catch statement

In this blog I am giving a very simple overview of the for statement of generally termed as the for loop. For loop is basically used for repeating a set of commands on any given data or set of points.  It starts with the word for and ends with the word end. The body of the for loop first contains the index. The index is a matrix. It can be unidimensional or multidimensional. It actually tells the for loop that how many times it has to repeat a certain operation. Every time an operation is carried out the index changes to the next forward point and again performs the operation till the last value of the index.

This can be well explained by an example. For example if we have a set of numbers from 1 to 10 and we want to square all these numbers. This would become very tedious if we start squaring the numbers one by one. The for loop does it in a very simple manner. First we define the set of numbers that we want to be squared, i.e a=1:1:10. The matrix a has now elements starting from 1 going forward with an increment of 1 and ending at 10.

Now in the next step we initiate the for loop and define the index which has a length equal to the length of the matrix a. This means MATLAB will repeat the operation for ten times. The operation to be repeated is defined in the next line where we say that square= a(i)^2. Then we terminate the for loop by the end command. For viewing the output we simply call the variable square in the last line.

This example is practically performed in my video. Refer to the link below for watching my video on For loop.

Thanks for your support and appreciation.

Written by: