From CSE100B Wiki
Vectors, Matrices, and Indexing
MATLAB stands for Matrix Laboratory, thus, the main focus and strength of MATLAB involves manipulating matrices. In this lab you will learn how to create matrices and access their elements. Let's get started.
Sequences are created using the colon (":") operator. For example, a row vector counting from 1 to 5 can be created with the command:
>> r = 1:5
1 2 3 4 5
We can also specify the interval which can be negative (the default interval is 1):
>>r = 2:-2:-8
2 0 -2 -4 -6 -8
|Q1: What is the last entry in the vector created by:|
Matrices are created using the square brackets (""). Here's a simple matrix, with 3 columns and 1 row:
>> m = [1 2 4]
1 2 4
This example shows how the space character is used for horizontal concatenation ("putting things next to each other"). Multiple spaces are treated as one.
The comma character also serves for horizontal concatenation (
[1 2 4]==[1,2,4]), but mutiple commas result in an error.
Vertical concatenation ("putting things on top of each other") is done using the semicolon (";"):
>> m = [1;2;4]
Combining the space/comma and the semicolon, we can create 2D matrices:
>> m = [1 2 3; 4 5 6; 7,8,9]
1 2 3
4 5 6
7 8 9
Here are some equivalent forms:
>> m = [[1 2 3]; [4 5 6]; [7 8 9]] % create three row vectors, and vertically-concatenate them
>> m = [[1; 4; 7] [2; 5; 8] [3; 6; 9]] % create three column vectors, and horizontally-concatenate them
>> m = [[[1 2; 4 5] [3; 6]]; [7 8 9]] % Start with 2x2. Append column vector (now 2x3). Append row vector (now 3x3)
>> x = [1 2; 4 5]
>> m = [[x [3; 6]]; [7 8 9]] % We can build matrices from existing matrices.
Note: The percent character ("%") causes everything following it on the same line to be ignored. This is called a comment. Use comments to explain pieces of code and document your scripts.
|lab1_1.m: (The work for this deliverable should be stored in a script named lab1_1.m)
Accessing Matrix Elements
Matrices are accessed using the round brackets ("()"), by specifying the desired row and column indices. To access an element that is in the i'th row and j'th column of matrix M, write M(i,j).
Note: Index in Matlab starts from 1 instead of 0, unlike some other programming languages.
REMEMBER: FIRST ROW, THEN COLUMN!
So, if we have the variable:rc =
11 12 13
21 22 23
31 32 33
To address the entry in the third row, second column (32) use rc(3,2).
|Q2: The following matrix represents the cash values (in millions of dollars) of safety deposit boxes arranged in a grid: |
If you could open one deposit box with the command depoboxes(i,j), what should i and j be to maximize your haul?
|Q3: After running lab1_2.m, what does |
return? (Hint: Remember the help command.)
Q4: After running lab1_2.m, what does
Using Vectors to Access Vectors
Suppose we have a row vector of size 5:
a = [92 83 74 65 0]
and we want to create a row vector of size 2, that contains only the second and fourth elements (in this case, b = [83 65]).
We already know one way to do it.
Since the second element is a(2), and the fourth element is a(4), then:
b = [a(2) , a(4)]
MATLAB allows us to use vectors as indices:
b = a([2 4])
If we wanted the first three elements (c = [92 83 54]), we could write:
c = a([1 2 3])
but we already know how to use the colon operator to create sequence (in this case [1 2 3]), so we can write:
c = a(1:3)
|Q5: With the vector a defined above, what command returns [0 65 74]? (Hint: Remember negative intervals.)|
Using The Colon Operator to Access Matrices
To access several elements of a matrix at a time, use the colon operator like this:
which returns the elements of matrix C in the first row, columns 2 through 5.
|Q6: What does this return? What does a colon (":") do in this instance?|
The colon is an important operator and you will use it often. Be sure you understand it!!
Let's play a word search game.
Using The Colon Operator to Update Matrices
Recall the matrix in Q2
depoboxes = [1 2 5; 4 8 99; 7 6 3]
After the money has been stolen from the deposit box, the bank must update the cash value.
To do this, the bank executes:
>> depoboxes(2,3) = 0
The owners of deposit boxes in row 3 have decided to move their money to another bank.
To do this, the bank executes:
>> depoboxes(3,:) = 0
|Q7: The bank has gone under. What command zeroes-out all of the deposit boxes?|
Indexing 2D Matrices with a Single Number
There is a way to index elements in a matrix with a single number (recall Q4).
MATLAB uses column-major indexing.
The following matrix has values corresponding to their column-major index, i.e., G(1) = 1, G(4) = 4, etc.
1 4 7
2 5 8
3 6 9
|Q8: The bank has been bailed out by the government and the original values have been returned. The bank keeps the keys to the boxes hanging on the wall in column-major order. (Look at the original depoboxes matrix and not G to answer these questions.) If the thief takes the fifth key, what is her pay-off? What key should the thief take to maximize her pay-off?|
When you are ready, demonstrate your answers to Q1-Q8 and lab1_1.m, lab1_2.m, and lab1_3.m to the TA or instructor and answer some questions to checkout.
Student Questions and Answers
Questions? Please post them on the Google Group http://groups.google.com/group/wustl-matlab.