Now you can also use for loops to access
elements of a matrix or of an array. We'll use it for an array, since that's
just one for loop instead of two. But what we're gonna do, is we're gonna look at how we use a for loop to add individual elements of an array. So you can say well I know how to do this because I'm just gonna say sum of A. You're like yeah I know, that's the right answer. But let's pretend that we didn't have this. And if you were programming in any other language besides MATLAB pretty much, well maybe. In a lot of different programming languages, you would either have to import a
special ,uh, header file that has this stuff in it, or you would have to write
your own function that would do this. So basically what we're going to do is we're gonna create a for loop that would add the values of this array. So actually I'd like you to give it a shot and see if you can figure out how to do it on your own. So positive you spend a little bit of time with it. If you're just totally stuck then come back, okay. So we're gonna assume that that happened.
I know that there are five elements in my array. I could just generalize it.
But for the moment I'm just gonna leave it. So I'm gonna say I'm going from one to five okay.
I don't have to use the word index, I just like it because it helps makes sense. So basically we're gonna do very similar to what we did whenever we used a for loop to just add numbers. But now we're going to add specific numbers. So whenever we're adding numbers, most of the time it's a good idea to start with initialization of the zero. So I'm gonna call this,
but I'm gonna initialize that to zero. And the reason I'm gonna initialize it to zero is that every time I'm going to add a number, I want it to originally add it to zero. So you're gonna if you're doing something over and over and over, if you're increasing a value. Then generally, what you're gonna see is some kind of a structure that looks like this. So bug equal bug plus, and I want to use these. The first time I go through, I want the first element of A. Then I want the second element of A, third element of A, and so on and so forth. So, literally all I need to do is I need to say, A at index. And believe it or not, that's all you need to do this. So we start off with bug being equal to zero.
And as we run through it, when index is 1, it's going to go zero plus, hang on a minute while I write this out. So, so bug is zero, and then index is one. And whenever index is one bug is going to equal to the current value bug plus A at the index. The index of 1. So 0 plus A 1 is 0 plus 8. So it's 8.
The second time I go through index is 2. So bug is equal to current value of bug, which is 8 plus A the current index. The index is 2. So it's 8 plus the current value at A 2,
which is 12. And that's 20. Okay. So, again if maybe you couldn't get the for loop, if you couldn't get the for loop, then it's really, really, really important that you pause the video and see if this makes sense. So go through all 5 steps, just to make sure that you're understanding what's going on. Especially if you couldn't write the for loop from scratch. The way you're gonna get good at it, is by practicing and doing stuff like this over and over. Okay. So I'm going to pretend that you pause the video, because you know learning is important. So I've got bug is getting both the previous value bug or the current value which is 20. Plus A at the current index. So 20 plus 57 which is 77, alright. Then when index is 4 I'm going to say, that bug is the current value bug plus A the current index. So 77 plus 19 which is some, i don't know, 96. I guess I could have done that, 96 I just don't want to get that wrong. And then index is equal to 5, then bug is equal to the current value bug plus A 5. Which means 96 plus A5 is 23, so 96 plus 23 is 119. So I hope to get a value of 119 whenever I run this. I can't tell what I did. So whenever I run it,
I get bug starts out at zero. Then it goes to 8 20 77 96 and 119. Which is precisely what I expected it to do. So I got the total sum of the elements of the index as 119. So that's it, yeah very exciting.
Here's another idea though. Let's say that instead of doing what we just did, let's say that we want to add like each pair of elements in the for loop. So instead of saying I want to add 8 and then 12.
I want to like add 8 and then add 8 and 12. And then add 12 and 57, 57 and 19 and 12 and 13.
I guess. I want to say I want to add in by pair. So I wanna add this one. And then I want to add
these two, and then I want to add these two, and then I want to add these two. Okay so this is gonna be a little bit more complicated and what I'd like to do is I'd like to just display. I'm just gonna display it. So my display should look like, it's not how it'd look. My display should look like it should say 20 and then it should say 69, and then it should say whatever 59, 57, and 19. Then it should say 76 and then it should
say whatever 19 and 23 is, 42 okay. So I just wanted to say, I just want to display to screen. I don't want to store it or anything like that.
So let's see how far we can get. So I just want to add the pairs of the elements within the loop. So whenever I did this I have to think, okay well do I want to think about starting with 8 and going to 19. So 1 to 4, or do I want to go to the 5. And it really just depends on your outlook on life. I am personally going to go from 1 to 4 because I like adding numbers instead subtracting.
So what I'm gonna do is, all I'm gonna do is every time I run through,
I'm going to take A at the current index and I'm going to add A to the index of the next one.That's it. That's all I need to do to make this work. So check this out. So I got 20 then I got
69 then I got 76 and then I got 42. So I have A at the current index
plus A at the current index plus 1. So the reason I didn't go to 5 is because whenever index is 5, it'd be 5 plus 1 is 6, and I don't have 6 elements in my array. If I run this it yelled at me. So that is a hint that I could go to 4. Now I said if you wanted you could actually go count down or from 2 to 5. And if you're going to go 2 to 5, then you would have to go like I probably say,
index minus 1 plus A at the current. So when the index is 2, it'd be 1 + 2 and 2 + 3 and so when the index is 5, it be 5 here and a 4 here, and I get the same display. I personally, like I said it just freaks me out just a teensy weensy a bit more, just enough that I would rather add.
Okay now I really want to blow your mind. Let's actually store these guys. So I'm gonna call, what I'm gonna do is I'm actually going to do this. I'm gonna say you know bug at index,
and I want you to see what this is going to do. So this is slowly going to create a matrix
or an array called bug. Where it is going to store the addition of each pair of elements of 20 69 to 76 to 42. Okay so I will kind of walk you through
what its gonna do. So index is equal to 1, it's gonna say bug at 1
is going to be equal to A at 1, plus A at 1 plus 1. Sorry 1 plus 1 so whatever that is.
So 8 plus 12 and that's 20. Alright. Then index is going to be 2.
So it's gonna say bug at the current index which is 2, is going to be equal to A at the current index plus A at the current index plus 1. That will give me the 12 plus 57
which is 69, and so on and so forth. So this is something that you also see happen a lot, is storing outputs into an array that you kind of create on the fly. Now it's actually yelling at you, because it says it appears to change size on every loop iteration, which is true. Considered preallocating for speed.
I'm not really worried about that right now. I'm just trying to teach for loops. So I can do that. Another thing that I can do to achieve the same thing. It's a little weird so I'm gonna put it here. This is just a little bit different, but you can actually use the end statement. So I say well instead of doing it the current index, just do it at the end of wherever bug is. The first time I do it, it's gonna yell at me, because there is no such thing as bug. And in this case I would have to initialize bug to a blank matrix. So I can initialize bug to a blank matrix and say, do an end. I think I'd do an end. I think I need to do it an end plus 1. So I could say do it wherever, wherever bug is go to the end of it and add a term. So these two things actually do the same thing. Personally I'm gonna gonna go with this guy, because it's a lot more obvious that what's happening. That said, sometimes this is the way you gotta go. To start out with the blank matrix and
slowly add to it.