This is a tutorial on how to write and use For Loops in MATLAB. Table of contents below.

00:00 – Introduction

00:30 – General form

00:57 – Principle of operation

01:40 – Example 1

02:35 – Example 2

03:43 – Example 3

05:11 – Example 3 variation

06:01 – Example 4

07:54 – Example 5

10:48 – Example 6

12:53 – Conclusion

Amazon Auto Links: No products found.

How do you make a matrix using for loops

How do you repeat a statement such as disturbance(b)? I have a vector b I want to disturb it 10 times but I do not know how set up a for loop for it

Hi…if I have row vector t=[0 0 -1 0 0] I have to sort in ascending order and then I have take 2 smallest value that is 0 and 0 then 0 will be -1 and other 0 will b 1.How to replace these elements and this will run till I get one positive number.

Can u please help me in this.

your result is from 1 to 55 , but my results are all 55

good one

thank you man, you really opened an ocean upon me.

I have an exam tomorrow, please everyone, pray for me.

How would you collect the result in the for loop that satisfies a certain condition?

Great Tutorial. Thank you very much.

Thank you for the awesome tutorial. I have an engineering final coming up and you helped immensely.

Would this work for modelling a solar system? I need to find a way of having the angle which the earth is respective to the sun change constantly (lets say everyday day, so 365 times) so can I use this to change both the cos and sin value at the same time?

Great video. I need to store in a vector generated in the n values is.

For example:

for i = 1: 0.1: 2

x = 2 * i

end

X only stores the last value obtained in the repeat loop. I need everyone … If not mess up, like I do?

5:02 why “sum_a = sum(a) + a(ii)”? why not only “sum_a = sum(a) + ii”?

index must be a

positive integer or logical. this is a error that i get how kan i fix it?

Can we change two variables in a MATLAB at same time by a for loop? Suppose t=1:10 and g=21:30. I want formula to run first for t=1 and g=21 then the formula should run for t=2 and g = 21,22. Then the formula should use t=3 and g=21,22,23 and so on and so forth till it takes t = 10 and g=21:30 (now all value of g). Can you please elaborate it a bit.

Please tell how do you put ‘%’ sign before each line after selecting them. thanks…

I don’t really understand the fourth example. can you help me with how the repetition is done?? like, what’s being done behind the scene by matlab for each output displayed. That would be of great help. Thanks beforehand.

Muito obrigado! it helps a lot!

Yeah Thanks, I have my first exam tomorrow morning and this helped me a lot

Hi. If i have n no. Of vector, can you pleas give me a for loop code that calculate the mean for this vectors and use it to calculate the covariance matrix.

Can any one help me in this pleas, Thank you so much.

You are a GEM! I would hug you if i can! Im a First timer in Coding (any Coding) and you made me understand it! Thank you!

for the following code, I am having issues with psi1 not going through every variable in the vector. I only get one output for theta when I should have 10. What am I doing wrong?

psi=[0 0.125 0.25 0.5 0.75 1 1.5 2 3 4];

for psi1 = psi

r=linspace(-5,5,10);

theta=asin((psi1.*r)/((r.^2)-1));

x=r*sin(theta);

y=r*cos(theta);

figure(3)

plot(x,y)

grid on

hold on

end

Hi, Ilya,

I have a question about one examples in your tutorial

why I use the same code

b = 1 : 10 ;

sum(b) = 0 ;

for i = 1 : length (b)

sum(b)= sum(b)+ b(i);

end

disp(sum(b))

and result shows a vector

for_loop

55 55 55 55 55 55 55 55 55 55

The natural numbers less than 10 that are multiples of 4 or 9 are 4, 8 and 9. Their sum is 21.Find the sum of all the multiples of 4 or 9 less than 1000. How would you find the matlab code for such a question? please help.

thanks helped us a lot

a=1:10

sum_a=0

for i=1:10

sum_a=sum_a + a(i)

end

I tried doing little variation in the example shown at 4:35 (fabonacci series). if we write above code, why does the first answer is calculated as zero.