Lesson 2.3: Accessing Parts of Matrix

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[MUSIC] Now that we know how to make a matrix, we need to know how to access parts of it. So, for example, if we want to see just the one element on the second row in the third column, we'll know how to do it. And it's all based on rows and columns. To see how it's done, we need a sample matrix. This command will make us one. And here it is. As you can see, its dimensions are 3 by 4, meaning that it has 3 rows and 4 columns. And they're numbered from top to bottom. 1, 2, 3. And the columns are numbered, too. 1, 2, 3, 4, from left to right. So how do you look at just the element on the second row and the third column? You give the command X, left parenthesis, 2, comma, 3, right parenthesis. The 2 is the row index, and the 3 is the column index. As always, the row comes before the column. Together, the two indices, 2 and 3, in parentheses and separated by commas, tell MATLAB which element you want to see. And it shows it to you. Okay, let's do this same example again, but this time, let's do it in MATLAB. Okay, well, let's look at this same example in MATLAB. We'll set X equal to left bracket, 1 colon 4, semi-colon, 5 colon 8, semi-colon, 9 colon 12, right bracket. Note how we've used the colon operator to save typing. You know, we could have done it the hard way, which would have been X equals 1, 2, 3, 4, 5, 6, you know, et cetera. I'm not even going to do that. It's just too much work. But the colon operator works perfectly. Each row of a matrix is simply a vector of numbers. And the colon operator produces a row vector of evenly spaced numbers. So if we want the numbers on a row to be evenly spaced, we can use a colon operator instead of typing them out. And that's what we wanted this time, so we used the colon operator. Okay, to continue with our example, let's specify the third element on the second row, as we did before. So, we say x, parenthesis now, 2, 3, right parenthesis. And we see the answer is 7. We always, as here we did, give the row index first and the column index second. We can also use this notation in an assignment operator. So let's say we want to call the variable center and we set it equal to X(2,3), as we did before. center now has the value 7. Or bob——I don't know——X[3,3]. There we go: bob is 11. And as you can see up here, where X was displayed——this 3 by 4 matrix—— on the third row——and here's the 3 down here—— in the third column—— here's the second 3——we find the 11. And we can assign a new value to a specified element. X(2,3), instead of being 7, I'd like it to be 97. So we set it to 97. You'll notice that while we only mentioned to MATLAB one element, it shows us the whole matrix. Well, we will notice, however, that it did make the change we requested. So element 2, 3, that is second row, third column, is now 97. And if we wanted the second row, second column to be—— well, I don't know——a hundred and twenty-three. If we look at it now, it's a hundred and twenty-three. So what happens if we assign a value to an element of a matrix, but the matrix doesn't exist? Let's say, for example, the matrix XYZ. If we look over in the Workspace, we'll see there's no XYZ matrix. So it doesn't exist yet. Another way to check to see if it exists is to try to look at it. Hm, we get one of those awful red messages saying we made some kind of error. Okay, MATLAB, I did this on purpose. It's not really an error this time. But anyway, let's set a value to it. XYZ, element 2,2 is equal to one hundred twenty-three. So we're doing the same thing we did with X, but now we're doing it with a non-existent matrix. So you expect more red to come back, I'm sure. Well, surprise! What is this? XYZ equals 0, 0, 0, 123. Well, here's what MATLAB did. It said: You want to have a matrix XYZ, and you want to have 123 on row 2, column 2. I'll give that to you. You didn't tell me what goes on the rest of the rows and columns, so I'll put zeros there. And that's what MATLAB does. It makes the smallest matrix it can and still accommodate your request. In other words, the smallest matrix it can that has an element, 2, 2. And that's a 2 by 2 matrix. And what happens if the matrix exists, but the element we specify doesn't exist? Let's try to peek at a non-existent element. Let's look at the element of the 3 by 4 matrix that's on the fourth row in the fifth column. That doesn't exist. Well, we get our hand slapped, and we deserve it. But an assignment to a non-existing position like this is another thing entirely. Let's assign a value to element 4,5, say 456. So what's this? Well, MATLAB has to extend the size of the matrix from 3 by 4 to 4 by 5 to accommodate our request to put something at element 4, 5: the 456. There it is. It's right there. We didn't tell it what else to put on the fourth row and we didn't tell it what else to put on the fifth column, so it put zeros there. There's one pitfall we need to point out concerning the first element on the first row. Let's set that first element on the first row to the value 99. It's 1 right now, and we'll change it to 99. There. So we see our matrix with the 99 replacing the 1. That works fine. You might be tempted to do this, because we've talked about how X as a scalar is the same as a 1 by 1 matrix. And it kind of looks like the same meaning here, but it's not. It's entirely different. What we have just done is obliterated our entire matrix and replaced it with just the 1 by 1 scalar matrix, 99. Well, we know how to access or change individual matrix elements. But, you know, we can change multiple elements at a time using the so-called subarray operations. Let's see how that works. Let's come up with a little simpler matrix this time, just two rows. It’s one of my favorite matrices because it's so easy to type. One, two, three, four, five, six. Let's look at a sub-array. I'm going to type X two comma and one three. What I've done here is I said that I'm interested in just the second row and I'm interested in the first and the third columns. I want to see that. So, MATLAB slices off that second row, and it gives me the first and the third element on the row, and there they are: four, six. This is a sub-array. This is the whole array, and this four and this six taking together is the subarray that we’ve specified. The comma's required, by the way: this one I'm talking about. Let's try doing without it. [CLICKING] We get an error. And it's just as easy to specify multiple rows as multiple columns. So here we go. I'm going to say I want row two first and then row one, and I just want to look at the column two. Now let's go back and look at what X is. It’s still visible up here. We've asked for row two first—and there's the five—and then row one—and there's the two. We asked for second column in each case. So it puts the five first and then the two. And here's a combination of multiple rows and multiple columns. From our rows, I'm going to say I want row two first, and then row one, and then row two, and then for the columns, I want to start with three and go to one and one again and then two. You can do anything you want to as long as you're picking elements that are actually inside the array. So let's just look briefly here. We took row two first, and row two had a four, a five, and a six on it. And we asked for columns three and then one twice and then two. So here the six, the four, the four is repeated because we asked for it twice, and then the 5, and then you can see what happened with the other rows. You can do any combination you want as long the elements you request exist in X. Let me give an up-arrow to repeat the previous command. I'm going to change that two to an eight and hit Enter. Well, MATLAB didn't like that because we asked for elements that didn't exist inside X. And you can use the colon operator to form the subarray. As an illustration, let's do this without the colon operator. I’m spacing this out so you can see it a little better. Or we can do the same result with the colon operator. Let's do it that way. Instead of this one, two, three vector which I wrote out explicitly, this time, I'm going to get the same thing with the colon operator. This not really a new concept. It's just a combination of two concepts that you’ve already learned. First, you learned that a vector like 1, 2, 3 can be used in a subarray operation. And second, you learn that the colon operator can produce a vector of evenly spaced numbers— in this case, one, two, three. [CLICK] And you can get as fancy as you want with this. For example, let's do this: We'll put two, colon, minus one, colon, one, for the rows we want and for the columns, three, colon, minus one, colon, one. We're going backwards on both of them. And there you have it. MATLAB provides a very helpful way for specifying the last index of a row or a column. Here's an example. [CLICKING] As you might guess, the word “end” here means the last row index in the matrix X. And here's how you ask for the last column index. I'm sure you can guess. You just put “end” in the column position. “end” is an example of a keyword in MATLAB. The term “keyword” is a computer science term, and it's used for all programming languages. Its definition is simply a word that has a special meaning in the language. And every programming language has keywords. Here, the special meaning of this keyword is last index. A keyword is also typically reserved, meaning that you can't use it as the name of a variable or a function. Let's try to do that. We'll set end equal to five, oops. As you can see MATLAB says it's a reserved word. The keyword “end” may not seem all that helpful to you right now, but we'll see later that it's very helpful when we write a program in which we can't know what the last index is. That actually happens a lot. When it does, “end” will always give it to us. Here's some more examples of end. [CLICKING] you can do it in both positions. [CLICKING] And here's two, end, one end three. So we pick the second last, the first, and then the last element again on the third column. And you might be surprised to find that “end” can be used in arithmetic expression. Subtraction's the most common. Let's do one of those. Let's remind ourselves what X is, now. It's one, two, three, four, five, six. So here we go: one, end minus one. So this means we want to be on the first row, and instead of the last element, we want one before the last element. end minus one means end, which is the index, three, minus one, which gives us the one before that too. And here's another one [CLICKING]. And finally, if you want the last two elements of the first row [CLICKING] in reverse order, you get them. Addition is a little bit less common with end, and it'll cause an error if you're using it to look beyond the end of a matrix or trying to, like say this: But it's perfectly legal in an assignment operation, like say this. [CLICK] Notice that X has gotten bigger. Up here, we see it was one, two, three, four, five, six, two rows, three columns. Now it's 1, 2, 3, 4, 5, 6, 17, 0, 0. When we put end plus 1, we said we wanted to put 17 on the third row. Well there wasn't a third row, so MATLAB added one. We told it what to put in the first column. We didn't tell it what to put in the other two columns. So as usual it guesses that we want zeroes there. “end” can also be used with a colon operator. Here's some examples of that. Let's see what we've done here. We want row one here, and we want to start at column one, and jump by two until we get to the end. Well, row one has a 1, 2, and a 3. We jump over that two and we get a 3, 1, 3. Here we want the third row, and here's the third row. And we want to start at the end and go backwards to 2, 0, 0 and there's the 0, 0. And in this last one, we want row 1—here's row 1. And we want to start, not at the end, but at one short of the end. That's here. And go to the end 2, 3. And so you see we have a 2, 3 here. Well, MATLAB also provides a shorthand for one particular phrase: 1 colon end. Let's start, first of all, by putting 1 colon n 2. 2, 5, 0. We want to go from the first row to the last row, and we want to stick on column two, and here it is. Let's look at this. Instead of 1 colon end, I just put colon, and then a 2. And it's the same thing. The colon stands for 1 colon end. It's a shorthand. And, of course, it's the same with the column indices. So we could do this. And that means we want row 1 and then all the elements on that row: 1, 2, 3. And, of course, you can use it in both positions. If we want all the rows and all the columns. That's just the same as X. So this isn't a shorthand. It's more like a longhand. And of course this is even longer. [CLICKING] But all three of these things give us the same thing. But the point here is that MATLAB opts for consistency, which means it always lets you use these expressions for any index with no exceptions. This emphasis on consistency is a hallmark of a good programming language, and all the programming languages that are in heavy use today, including C, C++, and Java, and even good old Fortran, all emphasize it too. Well, so far, we've used the array operations just to look at elements inside a matrix. But, you can also use them to change the values of elements inside a matrix. Let's look at an example of that. I'm going to give the subarray one to end and one. And if we look at that, we see that's elements 1, 4, and 17 there on the first column. And if I give this operation, which is an assignment operation, we see that we have assigned minus 44 to every one of the elements in that subarray, in other words, the whole first column. And you'll notice that as usual, we get to see the whole array when we do a subarray operation. Here's another one. Now, what we've done is told MATLAB that we're interested in all the rows and we're interested in columns two and three. And we want to set all those elements, each to the value of 9. Arrays can be on the right side too. Let me show what I'm talking about with an example. I'm going to type the same left side that I just typed. On the right side, I'm going to specify an array. There, you can see what's happened. The array that I specified on the right side just fits exactly into the subarray that I specified on the left side, and so MATLAB has dutifully put it there. And the size on the right side has to match that on the left side. It's a rule. Let's try to violate that rule and see what happens. Here on the left side, I'm putting exactly the same thing that I did before, but on the right side, I'm giving just a row vector, and that's not the same shape. And MATLAB complains. [MUSIC] [APPLAUSE]
Info
Channel: Fitzle LLC
Views: 83,770
Rating: 4.9283385 out of 5
Keywords: MATLAB (Programming Language)
Id: H_DIbljMpZs
Channel Id: undefined
Length: 21min 33sec (1293 seconds)
Published: Sat Jun 13 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.