# Matrices in Matlab

Topics: Matrix, Matrices, Vector space Pages: 24 (6867 words) Published: August 24, 2013
Section 2.2

Matrices in Matlab 75

2.2 Matrices in Matlab
You can think of a matrix as being made up of 1 or more row vectors of equal length. Equivalently, you can think of a matrix of being made up of 1 or more column vectors of equal length. Consider, for example, the matrix   1 2 3 0 A =  5 −1 0 0  . 3 −2 5 0 One could say that the matrix A is made up of 3 rows of length 4. Equivalently, one could say that matrix A is made up of 4 columns of length 3. In either model, we have 3 rows and 4 columns. We will say that the dimensions of the matrix are 3-by-4, sometimes written 3 × 4. We already know how to enter a matrix in Matlab: delimit each item in a row with a space or comma, and start a new row by ending a row with a semicolon. >> A=[1 2 3 0;5 -1 0 0;3 -2 5 0] A = 1 2 3 0 5 -1 0 0 3 -2 5 0 We can use Matlab’s size command to determine the dimensions of any matrix. >> size(A) ans = 3 4 That’s 3 rows and 4 columns!

Indexing
Indexing matrices in Matlab is similar to the indexing we saw with vectors. The diﬀerence is that there is another dimension 2. To access the element in row 2 column 3 of matrix A, enter this command.

1 2

Copyrighted material. See: http://msenux.redwoods.edu/Math4Textbook/ We’ll see later that we can have more than two dimensions.

76 Chapter 2

Vectors and Matrices in Matlab

>> A(2,3) ans = 0 This is indeed the element in row 2, column 3 of matrix A. You can access an entire row with Matlab’s colon operator. The command A(2,:) essentially means “row 2 every column” of matrix A. >> A(2,:) ans = 5 -1

0

0

Note that this is the second row of matrix A. Similarly, you can access any column of matrix A. The notation A(:,2) is pronounced “every row column 2” of matrix A. >> A(:,2) ans = 2 -1 -2 Note that this is the second column of matrix A. You can also extract a submatrix from the matrix A with indexing. Suppose, for example, that you would like to extract a submatrix using rows 1 and 3 and columns 2 and 4. >> A([1,3],[2,4]) ans = 2 0 -2 0 Study this carefully and determine if we’ve truly selected rows 1 and 3 and columns 2 and 4 of matrix A. It might help to repeat the contents of matrix A.

Section 2.2

Matrices in Matlab 77

>> A A = 1 5 3 2 -1 -2 3 0 5 0 0 0

You can assign a new value to an entry of matrix A. >> A(3,4)=12 A = 1 2 5 -1 3 -2

3 0 5

0 0 12

When you assign to a row, column, or submatrix of matrix A, you must replace the contents with a row, column, or submatrix of equal dimension. For example, this next command will assign new contents to the ﬁrst row of matrix A. >> A(1,:)=20:23 A = 20 21 22 5 -1 0 3 -2 5

23 0 12

There is an exception to this rule. If the right side contains a single number, then that number will be assigned to every entry of the submatrix on the left. For example, to make every entry in column 2 of matrix A equal to 11, try the following code. >> A(:,2)=11 A = 20 11 5 11 3 11

22 0 5

23 0 12

It’s interesting what happens (and very powerful) when you try to assign a value to an entry that has a row or column index larger than the corresponding dimension of the matrix. For example, try this command.

78 Chapter 2

Vectors and Matrices in Matlab

>> A(5,5)=777 A = 20 11 5 11 3 11 0 0 0 0

22 0 5 0 0

23 0 12 0 0

0 0 0 0 777

Note that Matlab happily assigns 777 to row 5, column 5, expanding the dimensions of the matrix and padding the missing entries with zeros. >> size(A) ans = 5 5

The Transpose of a Matrix
You can take the transpose of a matrix in exactly the same way that you took the transpose of a row or column vector. For example, form a “magic” matrix with the following command. >> A=magic(4) A = 16 2 5 11 9 7 4 14

3 10 6 15

13 8 12 1

You can compute AT with the following command. >> A.’ ans = 16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

Section 2.2

Matrices in Matlab 79

Note that the ﬁrst row of matrix AT was previously the ﬁrst column of matrix A. The...