FOR loops in MATLAB: Nested FOR loops

This screencasts gives an example of a nested FOR loop in a MATLAB program and how to create it based on a written algorithm. Also introduces the SIZE and MOD commands as a side trip.


Madeleine Foster says:

what does mod do??

Harshana Madusanka says:

Hi, -74% coupon to Learn Matlab course

Indian2020 says:

Sir, please tell me about loops that are used to take two inputs simultaneously and computing it: example-
fprintf(‘n take length 1:’);
fprintf(‘n take length 2:’)
%other computations

torqu3 says:

good job but speaking slower would make it easier to follow

VDEngineering says:

I am about to lose my mind right now
I’ve been trying for 4 hours to create a matrix with nested for loops
the matrix should have the output
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

Basically repeating a row n times, I am going crazy with these nested loops since I have to create a 50×50 matrix using that method in which element (1,1) is -5 and element(1,50) is -5, so
-5 -4.8 -4.6 …………
-5 -4.8 -4.6 …………
-5 -4.8 -4.6 …………
-5 -4.8 -4.6 …………

My code is as follows:
x(1,1) = -5;
%Loop to create the x matrix
for i = 1:50
    for j = 1:50
        x(i+1,j) = x(i,j) + 0.2;
        x(i+1,j) = x(i,j);

This constantly gives be a 2×1 matrix. What is going on, I don’t see ANYTHING wrong with this code

Please help, I am about to go insane

Mat Trimble says:

thank you so much

Rizwan Rifaqat says:

is it possible to save the resulting variables during the loop operation. for example for a loop from 1:5, i want to save all the generated variables during the iteration cycle under a different name. can it be done?

Insajder Papaj says:

I am an absolute beginner in programming and I find this video very useful! Thank you very much!

Anshuman Vyas says:

supercool,you are my hero…btw is it macbook or mac book pro..???

Trichy Dsp says:

for j= 2:2:rlen2r

for this code what is the value of this ” : ” , (k,:) Please give me details “?

zach greaves says:

say within a for loop defined as,
if …..

If I have a condition in the while loop that terminates the current history it is on and goes to the next history within the for loop… How would I do that? Say the condition is met in i=1, I want to terminate that loop if the neutron is lost and start, i=2.

Please help…

Keevon7 says:

Thank you!

Astonautumich says:

Hi there. there is more than one way.
– [1 2 3;4 5 6;7 8 9] each “;” starts a new row, so this end up being a (3*3) matrix.
– or use: a:b:c where a is your initial value, b is your step size, c is your final value.
– or use the command : magic(A) , if A=3 you end up having a 3*3 matrix and so on.

I hope this helps.

112rapture says:

Thank you for not having an accent

olger papa says:

Hello, i have wind speed data(per second) and i need a loop to calculate the average every 1 minute(60sec). 5.270.341rows 1column. Can you help me?

Kushagra Vashisht says:

your tutorial videos are really helpful and precise,keep up the good work!!

Felise Silva says:

what is the difference between using MOD and REM commands?

Strange Particl says:

that was a great lesson from you and I admired you way of explaining the example, I am welling to have a chat with you about somthing that I need the help in it. Welling to have you answer soon
Thank you in advance

Ben H says:

Just curious how to use MATLAB to find the values of a symbol?
Giving a+c=30 and 2<= c-a<=10 Both a and c are within a function and I need to find the min and max values of a? Any help would be greatly appreciated

Agust Hernan says:


Gusti Dermawan says:

hello sir, can you help me to complete my thesis?

Shyenne Bahar says:

Shouldn’t num_rows=size(A,2)?? Because the output of the function gives you the number of rows.

prince zachariah says:

How can a user enter an array of his own choice??What is the command for that? please answer

Hiren Suthar says:

MOD function is almost like rem Function which also calculates remainder I guess.

Alexander Maltagliati says:

I found this very helpful, but I felt that there were a few commands that were used but not fully explained:

“fprintf”, “%0.0f”, and “.n”

I do not know where these came from and what they do (aside from if they are used exactly the same as in this case).


Getnet Abebe says:

cool !

Segge Tauli says:

Molto interessante, suggerisco anche il corso grautito di “Corsi Consulenze NPR”,

Jenat Awad says:

wonderful , great job :-)

StealthShadowFox says:

Can you please show how to solve a matrix of a 5×3 and have it in increase in the order of small to large.

Example [ 1 2 5
15 18 23 ]

Thank you.

Mastela00 says:


Romi-Sana Murekar says:

Very well presented! I wish it were explained this clearly when I was learning it!

Sohil Shah says:

Gr8! Was very helpful!

Ahdan Fawwaz says:

Hi Sir, can you create Matlab program for 2 input and 2 output using if else statement? Thank you very much

Astonautumich says:

fprintf(‘Thank you so much’)

Eric Fox says:

Thank you and +10pts for sounding like an older Sheldon from Big Bang Theory

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