MATLAB For Loop Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this tutorial is about four loops in MATLAB for loops allow you to execute a block of code many times each time that the block is executed an index variable changes value for the duration of the current iteration for loops unlike other types of loops executes for a predetermined number of iterations this is useful if you would like to go through the elements of a matrix or vector one by one for example so let's look at the general structure first to start a for loop we type four then we have a loop index that variable that will change value for each iteration and we assign it equal to a vector then we have some code inside of the body of the for loop and we close it with an end statement the code will iterate a certain number of times that is determined by the length of the vector over here for each time that the code executes the loop index will take on the next value in the vector until there are no more values so for example if we have a vector that is 1 by 5 the loop will iterate 5 times with the loop index first taking the value of the first element of the vector and executing a code then the loop index takes on the second value of the vector and we execute this code block and so on until the loop index is the 5th value at which case the code will execute and then exit this for loop so let's look at an actual example let's comment this out let's take a look so we have for loop index we'll call it I I is equal to a vector 1 to 10 this has to be a row vector by the way and all we're going to do inside of the for loop is display the value of I I so I'll just type in I I the loop index and I'll leave off the semicolon so that the output is not suppressed if I run this code we can see our output on the right over here all it does is each time it prints what I is equal to a notice even though I don't actually change the value of I I manually within a loop 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 let's say we have a vector a equals negative five through five and we're going to go through each of those values and display them so we'll say for I I is equal to one through the length of the vector a I'll put a semicolon on this one here to suppress that output we're going through the values of a rather the indices of a and we're going to display the value of a at each of those index locations I I so again I'll a semicolon in order to show the output and we'll close that here so when we run this it just goes to each of the values of of a as you can see the first value is negative five next one is negative four negative three and so on up until five so we used the loop index I I as the index for this vector going through each of the values of the vector a one by one suppose now that we want to sum up the elements of the vector a again we can say let's say a is equal to one through ten we'll have a variable for the sum we call it a sum a and we'll initialize it to zero we'll say for I I is equal to 1 to length of a so again we're going to go through each of the elements of a one by one some a is going to be the previous value of sum a plus a of III was the current element of a and we'll end so this will go through every element of a and add it to the total sum at the end we can display a sum of a and I again omitted a semicolon so we're going to see a lot of output here so let me clear this screen just so it's more clear and I'll run this and here we go each time we add up another element of a to the sum of a and we see the first one is 1 next time we add a 2 to get a total of 3 next time we add 3 to get a total of 6 then we add 4 to get 10 and so on up until we've added all the elements and this is the output of the final dysfunction at the end so again we see that the for loop goes to each element of a because we told it to go from 1 through the lengths of a and we sum up the results we can also change this code a little bit in order to sum every other element for example our index does not have to go by once we could equivalently have done something like this where now we're going by tools and now we're going to sum every other element so in this case we're going to get of course a different result we run this and we're adding up every other element we first start with the first element of a but then this vector isn't going 1 2 3 4 5 6 7 8 9 10 now goes 1 3 5 7 9 so we're going to add up the first element of a which is 1 then we're going to add the third element of a which is 3 so we get a total of 4 then we add the next one which is the fifth so we had a 5 and so on and we get a total of 25 this time so this vector on the right the vector that we assign values from to the loop index can be anything it can be any vector at all in fact let me make a new example down here we can assign it to be a vector beforehand so let's say that a is again equal sorry 1 through 10 we'll say that we have a vector called end which is 1 4 9 3 and again we're going to sum up the elements of a so we'll initialize the some a variable and we'll say for I is equal to end we're going to set it equal to the vector that we declared beforehand and once again we have some a is equal to some a plus a of a I and then we have end and the end we can display some a once again so now when we run this let me clear the screen first we'll run this and here we go now what we're doing is we're adding up just the elements indexed by the numbers in this end vector so we first add up the first element of a which is one then the fourth which is a four so we add a 4 to this then the ninth so this makes 14 and finally we add the third element of a since the last element here is a three notice that the end vector does not have to be in any particular order just because before I declared everything in order for example 1 through length of a or 1 through 10 doesn't mean that it has to be in order it can be in any order as long as it's a valid row vector suppose now that we want to do something a little more complicated specifically we want to store the cumulative sum of a in another vector so let's comment this out and see how we'll do this so we'll start off with again a vector a and it can be anything so let's make it something different let's make it 20 through 54 and then we'll have a sum Veck which is going to be the cumulative sums so it's going to be a vector and we're going to pre-allocated two zeros 1 comma length of a so now we have this vector of zeros that's going to hold our some values the cumulative sum at each step we'll also have our sum a variable which is again equal to 0 we'll go for I I is equal to once again 1 through length of a since we're going to add up all the values of a let's say that some a is equal to some a plus a of a I just like before and some Veck of AI is going to be equal to some a just like that and that's it at the end we can do something like plot our result so now when we run this we get this is the result so this is the cumulative sum as it's being added up these are all the elements of a and it starts off with 20 and then it adds up 21 and 22 23 and so on up until 54 and here is the total sum and we can also view instead of this plot we can look at the actual vector 1 by 35 and just expected here are the values first 20 then we add 21 to make 41 then we add 20 to to make 63 and so on up until the end 1295 so in this case the code inside the body of the for loop isn't just one line but is two and again it can be arbitrarily long and we go through each of the values of the vector a no matter what it was because we index by this index I I which spans from 1 through the length of a regardless of what a is and we use this index not only to index a but also to store the appropriate value in the in the appropriate location in the sum vac cumulative sum vector so this is another way that for loops can be really useful one more example of for loops that I want to show is that of having money in a bank and seeing how it grows over time with a given interest rate so let's comment out this code and let's take a look how we would do this so we will start off with a balance of let's say $1,000 we want to see what's going to happen after 30 years so we'll say num years equals 30 our balance vector is going to be again zeros and it's going to be num years long and we'll go 4 and well this time we'll call our counter year to keep it more intuitive is equal to 1 through num years so 1 to 30 balance is going to be let's say we have an 8% interest rate per year so it's going to be 1.08 x balance and we'll fill in the balance vector at the appropriate year with the current balance we end that and again we can plot the result and that's it so let's run this code and here is what we get we start off with $1000 after one year we have a thousand times 1.08 and so on and it keeps growing as we expect up to the final value a year 30 and we can again take a look at the actual values in here over the 30 years and we end up with just over $10,000 at the end of 30 years so this is another place where we can very usefully use for loops again for loops we'll go through a given number of times and execute any block of code within the body so overall for loops are again extremely useful for going through vectors or matrices element by element and performing some kind of operations and storing those results of the operations in perhaps another results vector that's also indexed by the loop index the fact that the loop index changes value on every iteration is very useful because that means that you do not have to manually increment it yourself but it is important to keep track of what the loop index actually is and use it appropriately once again for loops are very very useful and you will see many uses of them throughout programming
Info
Channel: Ilya Mikhelson
Views: 566,693
Rating: 4.8554573 out of 5
Keywords: MATLAB (Programming Language), For Loop
Id: 1VKkuIRx-e0
Channel Id: undefined
Length: 13min 37sec (817 seconds)
Published: Sat Oct 12 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.