MATLAB For Loop Tutorial

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


Brian Moya says:

How do you make a matrix using for loops

Carl Näsvall Sindeby says:

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

Rose Roch says:

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.

Ronnie wang says:

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

Rameez Ajaz says:

good one

nightclock626 says:

thank you man, you really opened an ocean upon me.
I have an exam tomorrow, please everyone, pray for me.

Frias M. says:

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

pop4ti says:

Great Tutorial. Thank you very much.

Matthew Skarbinski says:

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

Diamond Blooded says:

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?

andrey montedori says:

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

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

Priyabrata Halder says:

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

Emy Antonius says:

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

Ather Cheema says:

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.

Abhideep Singh says:

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

Clarensky Benoit says:

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.

Raoni Barros says:

Muito obrigado! it helps a lot!

Backyard Experimenter says:

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

Monkey D Luffy says:

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.

The Boss says:

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!

Christopher Baldonado says:

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
grid on
hold on

Ronnie wang says:

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);

and result shows a vector
55 55 55 55 55 55 55 55 55 55

asfand khan says:

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

Abhideep Singh says:

for i=1:10
sum_a=sum_a + a(i)

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.

Write a comment


Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.

Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles   Show Cars   Motorbikes   Scooters   Bicycles   Rims & Tires   Luxury BoatsFashion   Sunglasses   Luxury Watches   Luxury Purses   Jeans Wear   High Heels   Kinis Swimwear   Perfumes   Jewellery   Cosmetics   Shaving Helpers   Fashion Hats   Modeling TipsFooding   Chef Club   Fooding Helpers   Coktails & LiquorsSports   Sport Shoes   Fitness & Detox   Golf Gear   Racquets   Hiking & Trek Gear   Diving Equipment   Ski Gear   Snowboards   Surf Boards   Rollers & SkatesEntertainment   DIY Guides   Zik Instruments   Published Books   Music Albums   Cine Movies   Trading Helpers   Make Money   Fishing Equipment   Paintball Supplies   Trading Card Games   Telescopes   Knives   Vapes   GameplaysHigh Tech   Flat Screens   Tech Devices   Camera Lenses   Audio HiFi   Printers   USB Devices   PC Hardware   Network Gear   Cloud Servers   Software Helpers   Programmer Helpers   Mobile Apps   Hearing AidsHome   Home Furniture   Home Appliances   Tools Workshop   Beddings   Floor Layings   Barbecues   Aquarium Gear   Safe Boxes   Office Supplies   Security Locks   Cleaning ProductsKids   Baby Strollers   Child Car Seats   Remote ControlledTravel   Luggages & Bags   Airlines Seats   Hotel Rooms   Fun Trips   Cruise Ships   Mexico Tours