Matlab

For Loop in Matlab Explained with Example Codes

Description:

For loop in Matlab:- In this tutorial, we will study about the for loop which is used to repeat a set of calculations a predefined number of times. First I will introduce you to the structure of a for loop and then I will walk you through an example in MATLAB. A loop is a structure for repeating a calculation or a set number of calculations a predefined number of times. Each repetition of a loop is known as a pass. The for loop is used when the number of passes is known in advance.

For loops in MATLAB

The typical structure of a for loop in Matlab, we start off with a loop variable which is used to determine the number of loop passes:

for variable = m:s:n
statement(s)
end


The loop variable has m is represent the initial value “s” is a step or incremental value and “n” represents the final or terminating value and for each value of the loop variable a statement or a number of statements are executed.
Let’s take a lookat an example:

for i= 2:2:4
x= i^2
end

In this case i is our loop variable i has an initial value of 2 an incremental or step value of 2 in a final or terminating value of 4 and for each value of I our statement is evaluated as x= i2.

Let’s take a look at a flow chart of our example which can help demonstrate each loop pass our loop variable i has an initial value of 2 and this initial value of I is not greater than our final value of 4 that statement is false and so we assign a value to x which is equal x= i2 now as the value of I = 2 so the value of x will be equal to 4. So that is the value assigned to x and that completes our first loop pass.

For Loop in Matlab

Next we increment I by 2 so i now has a value of 4 and we begin our second loop pass. Now our current value of i is 4 which is not greater than 4 the statement is false and now we assign a new value to x. So x is equal to x= i2 and as the value of I is now 4 so the square will become 16. So the value of 16 is now assigned to x that completes our second loop pass. Now we increment i by 2 i had a value of 4 we increment by 2 and the new value of i is equal to 6 and this begins our third and final loop pass and that is because our current value of i is equal to 6 which is greater than 4 that statement is true and our loop terminates with the endstatement.

Let’s take a look at implementing this example in MATLAB first we will start a new script file as we have discussed in the previous tutorial how to create script file. In the MATLAB editor we will create for loop which will start with the word for and then we will have a space and now we need to assign an initial value a step or incremental value and a final or terminating value to our loop variable.

For Loop in Matlab

So our loop variable is I and it is assigned so we do an equal sign the following values we start with our initial value which is 2 then we do a colon our incremental or our step value which is also two then a colon and our final or terminating value which is equal to four. Now our loop variable has been setup. So press the enter key which will start a new line that will be automatically indented .Now we are going to assign a value to x which is equal to x= i2. So i = 2 which will be the current value of the loop variable and then it’s squared. Now normally you would end this statement with a semicolon to suppress the output in this case I am going to remove that semicolon so the value of x is output to the command window. now I am going to hit enter and then our for loop ends with the word end which again is automatically indented.Now we are ready to run our program. so I am going to go to the top of the screen and click run.Now the program has run and you can see the output of the program in the command window at the bottom.

 

For Loop in Matlab



Now what you will see here is that we have two values for x one value is x was equal to 4 on the first pass of the loop and x was equal to16 on the second pass of the loop.Now on the third pass our loop variable had a value of 16 which was greater than our final or terminating value of 4 and that is why you do not see a third value assigned to x. In addition in the workspace on the right hand side you can see the final value for each of our variables so i had a final value of 4 before the loop terminated and x had a final value of 16 this completes our example.

For Loop in Matlab

Indexing and storage in arrays using for loop:

We are going to use a for loop to store data inside of an array using an array index. First I will introduce you to how arrays are handled in MATLAB then we will take a look at an example. We are going to write a script file to evaluate the function (y) where y is equal to:
y= x2
For x starting at a value of 0 and ending at a value of 8 in increments of 2.
X= 0, 2, 4, 6, 8
We are going to do this using a for loop. First we need to understand how MATLAB handles arrays a numerical array is an ordered collection of numbers and in this problem our independent variable x can be expressed as an array as follows:
X= [0, 2, 4, 6, 8]
In MATLAB square brackets are used to define an array and each element in a row array in this case is separated by a comma or a space. My personal preference is to use a comma to ensure readability and to prevent any errors. This array has five elements zero is our first element two our second element four is our third element six is our fourth element and eightis our fifth element to refer to a specific element in an array you can use an array index. For example x(3) refers to the third element in the arrayand in this case that third element has a value of 4. In this problem we want to calculate:
y= x2



For each value of x and this is an element by element operation so we don’t want to square the entire array we want to square each individual element. so we will square the first element then the second element,the third element, the fourth element and finally the fifth and final element. Now let’s take a look at how we will solve this problem using a for loop in MATLAB. So here is the traditional structure of a for loop:

for variable = m:s:n
statement(s)
end

and how we would use a for loop to solve this specific problem. Now let’s walk through each component of our program. First we initialize our variable x our loop variable in this case is equal to the array index i which has a starting value of 1a step or incremental value of 1and a final value of n. Where in this case the value of n is determined using the length function and the length function returns the number of elements in the row array x and in this case x has five elements 0 2 4 6 and 8.

x=[0, 2, 4, 6, 8 ];
n=length(x);
for i=1:1:n
y(i)=x(i)^2
end

and for each pass of our loop we will evaluate the following statement
y= x2
Looking at the code can be a bit confusing.

So let’s take a look at this flow chart which can help us visualize each loop pass. First we start by initializing our row array x and we determine the number of elements in the array x using the length function and there are five elements. Now let’s begin so our loop variable i which is also serving as our array index has a starting value of 1and a final value of n.

For Loop in Matlab


Where“n” has a value of 5 corresponding to the number of elements in x. Now our current value for i is not greater than our final value and we evaluate our statement.

x=[0, 2, 4, 6, 8 ];

So the first element in the array y is assigned the value of the first element in the array x squared and y now has a value of zero as x(1) represent the 0 value in the array x.

So we increment i our loop variable or array index by one i now has a value of two our current value of i is not greater than our final value and we begin our second loop pass. So we evaluate our statement again and the second element in the array y is assigned the value of the second element in the array x squared and our second element in the array y has a value of four because the second element in the array x is 2 whose square is equal to 4. Now we increment our loop variable by one and i has a value of three our current value of i is not greater than our final value this is false and we begin our third loop pass. Similar process can be occur for 3rd and 4th loop pass and now our fifth and final loop pass after we have completed our fifth loop pass our loop variable or our array index in this case has a value of six and this value is greater than our final value and that statement is true so our for loop ends.

Now let’s jump over to MATLAB and implement this problem now that we are in MATLAB the first thing we are going to do is to start a new script file and now that the editor is open we can begin our program our first step is going to be to initialize our row array x.

we use square brackets to enter in an array in MATLAB now we are going to enter our elements and we separate each column in this row array by a comma or a space again my personal preferenceis to use a comma to ensure readability and prevent any accidental errors I will end with another square bracket and then I am going to add asemi-colon to suppress this line.

x=[0, 2, 4, 6, 8 ];



So that is not output to the command window. Next I am going to set up our variable n and the purpose of this variable is to count the number of elements in the array x and we do that through the use of th elength function. So we do length of (x) and this will give us a value for n of five because there are five elements in the array.

n=length(x);

 Now we are ready to start our for loop so we start with for and then our loop variable i and in this case our loop variable is also serving as our array index our array index has a starting valueof 1 which would refer to the first element in the array a step or incremental size of 1and a final value of in next I am going to click enter and start our statement in this case we are evaluating y and what we want to do is to first on our initial loop pass calculate to the first element inside y and that first element in y is assigned the value of the first element inside the array x squared.

For Loop in Matlab

Now I am going to leave off the semicolon on this line so that y is output for each loop pass. Now I am going to click enter and type in end and this terminates our for loop. Now save the file the file and click on the RUN.




Now inside the command window if i scroll up i see the value of y for each loop pass so we have our first loop pass, our second loop pass, third, fourth, and our fifth and final loop pass and notice here that we have a value of y that corresponds to each element inside of our arrayx.

For Loop in Matlab

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button