For Loop Matlab Syntax Examples

Definition:

This tutorial is about loop Matlab. For loop in Matlab allows you to execute a block of code many times. For loops execute for a predetermined number of executions. This is useful if you would like to go for elements of the matrix one by one.

Let’s look at a general example first. To start a for loop we type

for loop_Index = vector;

code;

end

After execution, this code will iterate a certain number of times. Which is determined by the length of the vector. The loop index will take on the next value in the vector for each time the code executes until there are no more values left.

Example 1 for loop matlAB:

So, for instance, if we have a vector that is

Vector = [v1 v2 v3 v4 v5]

The for loop Matlab will iterate 5 times with the loop index first taken the value of the first element of the vector and executing the code. Then the for loop matlab index takes the second value while executing this code block. And so, on the loop index is the 5th value after which the code will execute and then exit this for a loop.

Let’s take an actual example

for ii= 1:10

ii

end.

Output:

I1 =

1

I2=

2

I3=

3

I4=

4

I5=

5

It prints what ii is equal to. It changes every time to each of the values of the vector over here.

Just as expected.

So, let’s look at a slightly more complicated example.

If you want help related to your Matlab Tasks Contact Us.

Example 2 FOR LOOP MATLAB:

Let’s say we have the vector

a = -5:5;

for ii =1: length(a)

a(ii)

end

output:

ans =

-5

ans =

-4

ans =

-3

ans =

-2

ans =

-1

ans =

0

ans =

1

ans =

2

ans =

3

ans =

4

ans =

5

So, we used the loop indexed ii as the index for this vector going through each of the values of the vector one by one.

If you want help related to your tasks related to Matlab Contact Us.

example 3 for loop matlab:

Suppose now that we want to sum up the elements of the vector a.

a=1:10;

sum_a = 0;

for ii = 1:length(a)

sum_a  = sum_a + a(ii)

end

disp(sum_a);

The output will be 55.

So it will go through every element, and add the total sum. So we see that for loop goes through every element of a.

We can also change this code in order to sum every other element for example our indexed is not have to go by 1.

a=1:10;

sum_a = 0;

for ii = 1:2:length(a)

sum_a  = sum_a + a(ii)

end

disp(sum_a);

Now it goes 1 3 5 7 9. So final output is 25.

Example 4:

a = 1:10;

ind = [1 4 9 3];

sum_a = 0;

for ii = ind

sum_a = sum _a + a (ii);

end

disp(sum_a)

output = 17

Now we are just adding up elements indexed by the numbers in this ind vector. Furthermore, we first add up the first element of a which is one and then 4 and then the 9 and then 3. The numbers in ind vector do not have to order.

Suppose now we want to do a little more complex thing.

We will start off with a vector a. Let’s make it 22:54 and then will have sum_vec which is going to be the cumulative sum and it’s going to be vector so we pre-allocate to zeros. We also have a sum of a variable that is again equal to zero.  We will go for ii is equal to one through the length of a so we are going to add up all the values of a.

a = 22:54;

sum_vec=zeros(1, length (a));

sum_a = 0

for ii = 1:length (a)

sum_a = sum_a + a(ii);

sum_vec(ii) = sum_a;

end

figure; plot(sum_vec)

Example 5:

One more example of for loops is that of having money in the bank and seeing how it grows over time with the given interest rate

We will start off with a balance of just say 1000$. Move on to see what’s going to happen after 30 years. Our balance factor is going to be again zeros it’s going to be num years long. Now we will call our counter as ‘years’. Bal is going to be let’s say we have 8% interest. We are filling the bal vector at the proper year with the current balance.

bal = 1000;

num_years = 30;

bal_vec = zeros (1, num_years);

for year = 1:num_years

bal = 1.08*bal;

bal_vec(year) = bal;

end

figure;plot(bal_vec)

In conclusion, this is another place where we can very usefully use for loop Matlab. Firstly, for loops, will go through a given number of times and execute any block of code within the body. So overall for loops are again very useful for going through vectors or matrices elements and performing some kind of operations and storing those results of the operation in perhaps another results vectors that are also indexed by the loop. Index the fact that the loop index changes the value on every iteration is very useful because that means that not have to manually increment it by yourself but it is important to keep track of what the loop index actually uses appropriately.

Once again for loops Matlab is very very useful and you will see many uses of them throughout programming.

Leave a Comment

Your email address will not be published.