Matrices in Matlab

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 ? . 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 2Copyrighted 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.

Get quality help now
Marrie pro writer
Verified

Proficient in: Algebra

5 (204)

“ She followed all my directions. It was really easy to contact her and respond very fast as well. ”

+84 relevant experts are online
Hire writer

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. gt;> 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. gt;> 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. gt;> 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 MatrixYou 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 second row of matrix AT was previously the second column of matrix A, and so on for the third and fourth columns of matrix AT . In essence, taking the transpose re? cts the matrix A across its main diagonal (upper left corner to lower right corner), so the rows of A become columns of AT and the columns of A become rows of AT . Building Matrices Matlab has some powerful capabilities for building new matrices out of one or more matrices and/or vectors. For example, start by building a 2 ? 3 matrix of ones. >> A=ones(2,3) A = 1 1 1 1 1 1 Now, build a new matrix with A as the ? rst column and A as the second column. As we are not starting a new row, we can use either space or commas to delimit the row entries. >> C=[A A] C = 1 1 1 1 1 1 1 1 1 1 1 1On the other hand, suppose that we want to build a new matrix with A as the ? rst row and A as the second row. To start a new row we must end the ? rst row with a semicolon. >> C=[A; A] C = 1 1 1 1 1 1 1 1 1 1 1 1 Let’s create a 2 ? 3 matrix of all zeros. 80 Chapter 2 Vectors and Matrices in Matlab >> D=zeros(2,3) D = 0 0 0 0 0 0 Now, let’s build a matrix out of the matrices A and D. >> E=[A D;D A] E = 1 1 1 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 The possibilities are endless, with one caveat. The dimensions must be correct or Matlab will report an error. For example, create a 2 ? 2 matrix of ones. gt;> A=ones(2,2) A = 1 1 1 1 And a 2 ? 3 matrix of zeros. >> B=zeros(2,3) B = 0 0 0 0 0 0 It’s possible to build a new matrix with A and B as row elements. >> C=[A B] C = 1 1 1 1 0 0 0 0 0 0 Section 2. 2 Matrices in Matlab 81 But it’s not possible to build a new matrix with A and B as column elements. >> C=[A;B] ??? Error using ==> vertcat CAT arguments dimensions are not consistent. This happens because A has 2 columns, but B has 3 columns, so the columns don’t line up. We’ll see in later work that the matrix building capabilities of Matlab are a powerful ally. Scalar-Matrix MultiplicationIf asked to multiply a matrix by a scalar, one would hope that the operation of scalar-matrix multiplication would be carried out in exactly the same manner as scalar-vector multiplication. That is, simply multiply each entry of the matrix by the scalar. Example 1. If A is the matrix ? 1 2 3 A = 3? 4 5 6? , 7 8 9 ? perform the scalar-matrix multiplication 3A. Simply multiply 3 times each ? 1 3A = 3 ? 4 7 entry of the matrix. ? ? ? 2 3 3 6 9 5 6 ? = ? 12 15 18 ? 8 9 21 24 27 Matlab understands scalar-matrix multiplication. First, enter matrix A. >> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 Now compute 3A. 82 Chapter 2Vectors and Matrices in Matlab >> 3*A ans = 3 12 21 6 15 24 9 18 27 Matrix Addition If two matrices have the same dimension, then add the matrices by adding the corresponding entries in each matrix. Example 2. If A and B are the matrices ? ? ? ? 1 1 1 1 1 1 A = ? 2 2 2? and B = ? 1 1 1? , 3 3 3 1 1 1 ? nd the sum A + B. Simply add the corresponding entries. ? ? ? ? ? ? 1 1 1 1 1 1 2 2 2 A + B = ? 2 2 2? + ? 1 1 1? = ? 3 3 3?. 3 3 3 1 1 1 4 4 4 Matlab understands matrix addition. >> A=[1 1 1;2 2 2;3 3 3]; B=[1 1 1;1 1 1;1 1 1]; >> A+B ans = 2 2 2 3 3 3 4 4 4 This is identical to the hand-calculated sum above.Let’s look what happens when the dimensions are not the same. Example 3. If A and B are the matrices Section 2. 2 ? 1 1 1 A = ? 2 2 2? 3 3 3 ? then ? nd the sum A + B. Note the dimensions of each matrix. Matrices in Matlab 83 and B= 1 1 1 , 1 1 1 >> A=[1 1 1;2 2 2;3 3 3]; B=[1 1 1;1 1 1]; >> size(A) ans = 3 3 >> size(B) ans = 2 3 The matrices A and B do not have the same dimensions. Therfore, it is not possible to sum the two matrices. >> A+B ??? Error using ==> plus Matrix dimensions must agree. This error message is completely expected. One ? nal example is in order. Example 4. If matrix A is ? 1 1 1 A = ? 2 2 2? 3 3 3 compute A + 1. Note that this addition of a matrix and a scalar makes no sense. ? ? 1 1 1 A + 1 = ? 2 2 2? + 1 3 3 3 ? 84 Chapter 2 Vectors and Matrices in Matlab The dimensions are all wrong. However, this is such a common occurrence in algebraic calculations (as we will see throughout the course), Matlab allows this matrix-scalar addition. >> A=[1 1 1;2 2 2;3 3 3]; >> A+1 ans = 2 2 2 3 3 3 4 4 4 Matlab simply adds 1 to each entry of the matrix A. That is, Matlab interprets A + 1 as if it were the matrix addition of Example 2. Matrix addition enjoys several properties, which we will ask you to explore in the exercises. . Addition is commutative. That is, A + B = B + A for all matrices A and B having the same dimension. 2. Addition is associative. That is, (A + B) + C = A + (B + C), for all matrices A, B, and C having the same dimension. 3. The zero matrix is the additive identity. That is, if A is m ? n and 0 is an m ? n matrix of all zeros, then A + 0 = A. 4. Each matrix A has an additive inverse. Form the matrix ? A by negating each entry of the matrix A. Then, A + (? A) = 0. Matrix-Vector Multiplication Consider the linear system of three equations in three unknowns. 2x + 3y + 4z = 6 3x + 2y + 4z = 8 5x ? 3y + 8x = 1. 2. 1) Because each of the corresponding entries are equal, the following 3 ? 1 vectors are also equal. ? ? ? ? 2x + 3y + 4z 6 ? 3x + 2y + 4z ? = ? 8 ? 5x ? 3y + 8x 1 Section 2. 2 Matrices in Matlab 85 The left-hand vector can be written as a vector sum. ? ? ? ? ? ? ? ? 2x 3y 4z 6 ? 3x ? + ? 2y ? + ? 4z ? = ? 8 ? 5x ? 3y 8z 1 Scalar multiplication can be used to factor the variable out of each vector on the left-hand side. ? ? ? ? ? ? ? ? 2 3 4 6 x? 3? + y? 2 ? + z? 4? = ? 8? (2. 2) 5 ? 3 8 1 The construct on the left-hand side of this result is so important that we will pause to make a de? nition.Definition 5. Let ? 1 , ? 2 , . . . , and ? n be scalars and let v1 , v2 , . . . , and vn be vectors. Then the construction ? 1 v1 + ? 2 v2 + · · · + ? n vn is called a linear combination of the vectors v1 , v2 , . . . , and vn . The scalars ? 1 , ? 2 , . . . , and ? n are called the weights of the linear combination. For example, we say that ? ? ? ? ? ? 2 3 4 ? 3? + y? 2 ? + z? 4? x 5 ? 3 8 is a linear combination of the vectors [2, 3, 5]T , [3, 2, ? 3]T , and [4, 4, 8]T . 3 Finally, we take one last additional step and write the system (2. 2) in the form ? ?? ? ? ? 2 3 4 x 6 ? 3 2 4?? y ? = ? 8?. (2. 3) 5 ? 8 z 1 Note that the system (2. 3) has the form Ax = b, where 3 Here we use the transpose operator to save a bit of space in the document. 86 Chapter 2 ? Vectors and Matrices in Matlab ? ? x ? y ? , x= z ? ? 6 ? 8?. b= 1 ? 2 3 4 A = ? 3 2 4? , 5 ? 3 8 and The matrix A in (2. 3) is called the coe? cient matrix. If you compare the coe? cient matrix in (2. 3) with the original system (2. 1), you see that the entries of the coe? cient matrix are simply the coe? cients of x, y, and z in (2. 1). On right-hand side of system (2. 3), the vector b = [6, 8, 1]T contains the numbers on the right-hand side of the original system (2. ). Thus, it is a simple matter to transform a system of equations into a matrix equation. However, it is even more important to compare the left-hand sides of system (2. 2) and system (2. 3), noting that ? ?? ? ? ? ? ? ? ? 2 3 4 x 2 3 4 ? 3 2 4?? y ? = x? 3? + y? 2 ? + z? 4?. 5 ? 3 8 z 5 ? 3 8 This tells us how to multiply a matrix and a vector. One takes a linear combination of the columns of the matrix, using the entries in the vector as weights for the linear combination. Let’s look at an example of matrix-vector multiplication Example 6. Multiply the matrix and vector ?? ? ? 1 2 ? 3 1 ? 3 0 4 ? ? ? 2 ? . 0 ? 2 3 To perform the multiplication, take a linear combination of the columns of the matrix, using the entries in the vector as weights. ?? ? ? ? ? ? ? ? ? 1 2 ? 3 1 1 2 ? 3 ? 3 0 4 ? ? ? 2 ? = 1 ? 3 ? ? 2 ? 0 ? + 3 ? 4 ? 0 ? 2 2 3 0 ? 2 2 ? ? ? ? ? ? 1 ? 4 ? 9 ? 3 ? + ? 0 ? + ? 12 ? = 0 4 6 ? ? ? 12 ? 15 ? = 10 It’s important to note that this answer has the same number of entries as does each column of the matrix. Section 2. 2 Matrices in Matlab 87 Let’s see if Matlab understands this form of matrix-vector multiplication. First, load the matrix and the vector. >> A=[1 2 -3;3 0 4;0 -2 2]; x=[1; -2; 3]; Now perform the multiplication. gt;> A*x ans = -12 15 10 Note this is identical to our hand calculated result. Let’s look at another example. Example 7. Multiply Ax, where A= 1 1 1 2 0 ? 2 and x = 1 . 1 If you try to perform the matrix-vector by taking a linear combination using the entries of the vectors as weights, Ax = 1 1 1 2 0 ? 2 1 1 1 1 =1 +1 +? . 1 2 0 ? 2 (2. 4) The problem is clear. There are not enough weights in the vector to perform the linear combination. Let’s see if Matlab understands this “weighty” problem. >> A=[1 1 1;2 0 -2]; x=[1; 1]; >> A*x ??? Error using ==> mtimes Inner matrix dimensions must agree.Inner dimensions? Let’s see if we can intuit what that means. In our example, matrix A has dimensions 2 ? 3 and vector x has dimensions 2 ? 1. If we juxtapose these dimensions in the form (2? 3)(2? 1), then the inner dimensions don’t match. 88 Chapter 2 Vectors and Matrices in Matlab Dimension Requirement. If matrix A has dimensions m ? n and vector x has dimensions n ? 1, then we say “the innner dimensions match,” and the matrix-vector product Ax is possible. In words, the number of columns of matrix A must equal the number of rows of vector x. Matrix-Matrix Multiplication We would like to extend our de? ition of matrix-vector multiplication in order to ? nd the product of matrices. Here is the needed de? nition. Definition 8. Let A and B be matrices and let b1 , b2 , . . . , and bn represent the columns of matrix B. Then, AB = A b1 , b2 , . . . , bn = Ab1 , Ab2 , . . . , Abn . Thus, to take the product of matrices A and B, simply multiply matrix A times each vector column of matrix B. Let’s look at an example. Example 9. Multiply 1 2 3 4 1 ? 2 . 2 1 We multiply the ? rst matrix times each column of the second matrix, then use linear combinations to perform the matrix-vector multiplications. 1 2 3 4 1 ? = 2 1 = 1 = 1 2 3 4 1 , 2 1 2 3 4 ? 2 1 1 2 1 2 +2 , ? 2 +1 3 4 3 4 5 0 11 ? 2 Let’s see if Matlab understands this form of matrix-matrix multiplication. First, load the matrices A and B. >> A=[1 2;3 4]; B=[1 -2;2 1]; Now, multiply. Section 2. 2 Matrices in Matlab 89 >> A*B ans = 5 11 0 -2 Note that this result is indentical to our hand calculation. Again, the inner dimensions must match or the matrix-matrix multiplication is not possible. Let’s look at an example where things go wrong. Example 10. Multiply 1 1 1 2 0 ? 2 1 2 . 3 4 When we multiply the ? rst matrix times each column of the second matrix, we immediately see di? ulty with the dimensions. 1 1 1 2 0 ? 2 1 2 = 3 4 1 1 1 2 0 ? 2 1 , 3 1 1 1 2 0 ? 2 2 4 (2. 5) In the ? rst column of the matrix product, the matrix-vector multiplication is not possible. The number of columns of the matrix does not match the number of entries in the vector. Therefore, it is not possible to form the product of these two matrices. Let’s see if Matlab understands this dimension di? culty. >> A=[1 1 1;2 0 -2]; B=[1 2;3 4]; >> A*B ??? Error using ==> mtimes Inner matrix dimensions must agree. The error message is precisely the one we would expect. Dimension Requirement.If matrix A has dimensions m ? n and matrix B has dimensions n ? p, then we say “the inner dimensions match,” and the matrix-matrix product AB is possible. In words, the number of columns of matrix A must equal the number of rows of matrix B. Let’s look at another example. 90 Chapter 2 Vectors and Matrices in Matlab Example 11. Multiply ? 1 2 1 1 1 ? AB = 1 ? 2 ? . 2 0 ? 2 2 0 Load the matrices A nd B into Matlab and check their dimensions. >> A=[1 1 1;2 0 -2]; B=[1 2;1 -2;2 0]; >> size(A) ans = 2 3 >> size(B) ans = 3 2 Thus, matrix A has dimensions 2 ? 3 and B has dimensions 3 ? . Therefore, the inner dimensions match (they both equal 3) and it is possible to form the product of A and B. >> C=A*B C = 4 -2 ? 0 4 Note the dimensions of the answer. >> size(C) ans = 2 2 Recall that A was 2 ? 3 and B was 3 ? 2. Note that the “outer dimensions” are 2 ? 2, which give the dimensions of the product. Dimensions of the Product. If matrix A is m ? n and matrix B is n ? p, then the dimensions of AB will be m ? p. We say that the “outer dimensions give the dimension of the product. ” Section 2. 2 Matrices in Matlab 91 Properties of Matrix MultiplicationMatrix multiplication is associative. That is, for any matrices A, B, and C, providing the dimensions are right, (AB)C = A(BC). Let’s look at an example. Example 12. Given A= 2 2 , 3 3 B= 1 1 , 2 5 and C = 3 3 , 2 5 use Matlab to demonstrate that (AB)C = A(BC). Load the matrices A, B, and C into Matlab, then calculate the left-hand side of (AB)C = A(BC). >> A=[2 2;3 3]; B=[1 1;2 5]; C=[3 3;2 5]; >> (A*B)*C ans = 42 78 63 117 Next, calculate the right-hand side of (AB)C = A(BC). >> A*(B*C) ans = 42 78 63 117 Hence, (AB)C = A(BC). Matrix Multiplication is Associative.In general, if A, B, and C have dimensions so that the multiplications are possible, matrix multiplication is associative. That is, it is always the case that (AB)C = A(BC). 92 Chapter 2 Vectors and Matrices in Matlab Unfortunately, matrix multiplication is not commutative. That is, even if A and B are of correct dimensions, it is possible that AB = BA. Let’s look at an example. Example 13. Let A= 1 2 3 4 and B = 3 5 . 2 7 Do the matrices A and B commute? That is, does AB = BA? Load the matrices into Matlab, then compute AB. >> A=[1 2;3 4]; B=[3 5;2 7]; >> A*B ans = 7 19 17 43 Now compute BA. >> B*A ans = 18 23 6 32 Thus, AB = BA. Matrix Multiplication is not Commutative. In general, even if the dimensions of A and B allow us to reverse the order of multiplication, matrices A and B will not commute. That is, AB = BA. Any change in the order of multiplication of matrices will probably change the answer. Some matrices do commute, making this even more complicated. Section 2. 2 Matrices in Matlab 93 >> A=[5 3;7 4],B=[-4 3;7 -5]; >> A*B ans = 1 0 0 1 >> B*A ans = 1 0 0 1 In this case, AB = BA. However, in general, matrix multiplication is not commutative. The loss of the commutative property is not to be taken lightly.Any time you change the order of multiplication, you are risking an incorrect answer. There are many insidious ways that changes of order can creep into our calculations. For example, if you multiply the left-hand side of equation on the left by a matrix A, then multiply the right-hand side of the equation on the right by the same matrix A, you’ve changed the order and should expect an incorrect answer. We will explore how the loss of the commutative property can adversely a? ect other familiar algebraic properties in the exercises. Here is a list of matrix properties you can depend on working all of the time.Let A and B be matrices of the correct dimension so that the additions and multiplications that follow are possible. Let ? and ? be scalars. A(B + C) = AB + AC (A + B)C = AC + BC. (? + ? )A = ? A + ? A ? (A + B) = ? A + ? B. ?(? A) = (?? )A. (? A)B = ?(AB) = A(? B). For example, as stated above, matrix multiplication is distributive over addition. That is, A(B + C) = AB + AC. >> A=[2 3;-1 4]; B=[1 2;0 9]; C=[-3 2;4 4]; >> A*(B+C) ans = 8 47 18 48 >> A*B+A*C ans = 8 47 18 48 94 Chapter 2 Vectors and Matrices in Matlab We will explore the remaining properties in the exercises. Section 2. 2 Matrices in Matlab 95 2. Exercises 1. Given the matrices A= and C= 3 1 , 5 8 3 3 , 2 1 B= 1 1 , 2 3 and C= 1 2 , 0 9 3. Given the matrices A= 1 0 , 2 5 B= 0 1 , 2 7 use Matlab to verify each of the following properties. Note that 0 represents the zero matrix. a) A + B = B + A b) (A + B) + C = A + (B + C) c) A + 0 = A d) A + (? A) = 0 use Matlab to verify each of the following forms of the distributive property. a) A(B + C) = AB + AC b) (A + B)C = AC + BC 4. Given the matrices A= 2 2 , 4 7 B= 3 1 , 8 9 2. The fact that matrix multiplication is not commutative is a huge loss. For example, with real numbers, the following familiar algeraic properties hold. . = ii. (a + b)2 = a2 + 2ab + b2 iii. (a + b)(a ? b) = a2 ? b2 Use Matlab and the matrices A= 1 1 4 2 and B = 2 3 1 6 (ab)2 a2 b2 and the scalars ? = 2 and ? = ? 3, use Matlab to verify each of the following properties. a) (? + ? )A = ? A + ? A b) ? (A + B) = ? A + ? B c) ? (? A) = (?? )A d) (? A)B = ? (AB) = A(? B) 5. Enter the matrices A=pascal(3) and B=magic(3). a) Use Matlab to compute (A+B)T . b) Use Matlab to compute AT + B T and compare your result with the result from part (a). Explain what your learned in this exercise. to show that none of these properties is valid for these choices of A and B.Can you explain why each of properties (i-iii) is not valid for matrix multiplication? Hint: Try to expand the left-hand side of each property to arrive at the right-hand side. 96 Chapter 2 Vectors and Matrices in Matlab a) What is the result of the Matlab command A(:,2)=[ ]? Note: [ ] is the empty matrix. b) Refresh matrix A with A=pascal(4). What is the result of the Matlab command A(3,:)=[ ]? 13. Enter the matrix A=pascal(5). a) What command will add a row of all ones to the bottom of matrix A? Use Matlab to verify your conjecture. b) What command will add a column of all ones to the right end of matrix A?Use Matlab to verify your conjecture. 14. Enter the matrix A=magic(3). Execute the command A(5,4)=0. Explain the resulting matrix. 15. Enter the matrix A=ones(5). a) Explain how you can insert a row of all 5’s betwen rows 2 and 3 of matrix A. Use Matlab to verify your conjecure. b) Explain how you can insert a column of all 5’s betwen columns 3 and 4 of matrix A. Use Matlab to verify your conjecure. 16. Enter the matrix ? ? 1 2 3 A = ? 4 5 6?. 7 8 9 a) What is the output of the Matlab command A=A([1,3,2],:)? 6. Enter the matrix A=pascal(4) and the scalar ? = 5. a) Use Matlab to compute (? A)T . b) Use Matlab to compute ?A and compare your result with the result from part (a). Explain what your learned in this exercise. 7. Using hand calculations only, calculate the following matrix-vector product, then verify your result in Matlab. ? ?? ? 1 1 2 1 ? 3 4 0?? 2 ? 0 5 6 ? 5 8. Write the following system of linear equations in matrix-vector form. 2x + 2y + 3z = ? 3 4x + 2y ? 8z = 12 3x + 2y + 5z = 10 9. Using hand calculations only, calculate the following matrix-matrix product, then verify your result in Matlab. ? ?? ? 2 3 1 1 1 4 ? 0 1 2?? 0 0 5? 0 0 5 3 5 2 10. Enter the matrix magic(8). What Matlab command will zero out all of the even rows?Use Matlab to verify your conjecture. 11. Enter the matrix pascal(8). What Matlab command will zero out all of the odd columns? Use Matlab to verify your conjecture. 12. Enter the matrix A=pascal(4). Section 2. 2 b) Refresh matrix A to its original value. What Matlab command will swap columns 1 and 3 of matrix A? Use Matlab to verify your conjecture. 17. Enter the matrix ? ? 1 2 3 A = ? 4 5 6?. 7 8 9 a) Enter the Matlab command A(2,:)=A(2,:)-4*A(1,:)? Explain the result of this command. b) Continue with the resulting matrix A from part (a). What is the output of the Matlab command A(3,:)=A(3,:)-7*A(1,:)?Explain the result of this command. 18. Type format rat to change the display to rational format. Create a 3 ? 3 Hilbert matrix with the command H=hilb(3). a) What is the output of the Matlab command H(1,:)=6*H(:,1)? Explain the result of this command. b) Continue with the resulting matrix H from part (a). What command will clear the fractions from row 2 of this result? 19. Enter the matrices A=magic(3) and B=pascal(3). Execute the command C=A+i*B. Note: You may have to enter clear i to return i to its default (the square root of ? 1). a) What is the transpose of the matrix C? Use Matlab to verify your Matrices in Matlab 97 esponse. b) What is the conjugate transpose of the matrix C? Use Matlab to verify your response. 20. Use Matlab’s hadamard(n) command to form Hadarmard matrices of order n = 2, 4, 8, and 16. In each case, use Matlab to calculate H T H. Note the pattern. Explain in your own words what would happen if your formed the matrix product H T H, where H is a Hadamard matrix of order 256. 21. Enter the Matlab command magic(n) to form a “magic” matrix of order n = 8. Use Matlab’s sum command to sum both the columns and the rows of your “magic” matrix. Type help sum to learn how to use the syntax sum(X,dim) to accomplish this goal.What is “magic” about this matrix? 22. Enter the Matlab command A=magic(n) to form a “magic” matrix of order n = 8. Use Matlab’s sum command to sum the columns of your “magic” matrix. Explain how you can use matrix-vector multilication to sum the columns of matrix A. 23. Set A=pascal(5) and then set I=eye(5), then ? nd the matrix product AI. Why is I called the identity matrix? Describe what a 256 ? 256 identity matrix would look like. 24. Set A=pascal(4) and then set B=magic(4). What operation will produce the second column of the matrix product AB? Can this be done 98 Chapter 2 Vectors and Matrices in Matlab 28.Enter the Matlab command hankel(x) to form a Hankel matrix H, where x is the vector [1, 2, 3, 4]. The help ? le for the hankel commands describes the Hankel matrix as a symmetric matrix. Take the transpose of H. Describe what is mean by a symmetric matrix. 29. A Hilbert matrix H is de? ned by H(i, j) = 1/(i + j ? 1), where i ranges from 1 to the number of rows and j ranges from 1 to the number of columns. Use this de? nition and hand calculations to ? nd a Hilbert matrix of dimension 4 ? 4. Use format rat and Matlab’s hilb command to check your result. 30. The number of ways to choose k objects from a set of n objects is de? ed and calcualted with the formula n k = n! . k! (n ? k)! without ? nding the product AB? 25. Set the vector v=(1:5). ’ and the vector w=(2:6). ’. a) The product vT w is called an inner product because of the position of the transpose operator. Use Matalb to compute the inner product of the vectors v and w. b) The product vwT is called an outer product because of the position of the transpose operator. Use Matalb to compute the outer product of the vectors v and w. 26. Enter A=[0. 2 0. 6;0. 8 0. 4]. Calculate An for n = 2, 3, 4, 5, etc. Does this sequence of matrices converge? If so, to what approximate matrix do they converge? 7. Use Matlab ones command to create the matrices ? ? 2 2 2 1 1 A= , B = ? 2 2 2? , 1 1 2 2 2 and 3 3 . 3 3 Craft a Matlab command that will build the block diagonal matrix ? ? A 0 0 C = ? 0 B 0 ? , 0 0 C where the zeros in this matrix represent matrices of zeros of the appropriate size. De? ne a Pascal matrix P with the formula P (i, j) = i+j? 2 , i? 1 where i ranges from 1 to the number of rows and j ranges from 1 to the number of columns. Use this de? nition and hand calculations to ? nd a Pascal matrix of dimension 4 ? 4. Use Matlab’s pascal command to check your result. Section 2. 2 Matrices in Matlab 99 2. 2 Answers 1. ) Enter the matrices. >> A=[3 3;2 1]; B=[1 1;2 3]; Calculate A + B. >> A+B ans = 4 4 >> A+(B+C) ans = 7 5 9 12 c) Enter the matrix A and the zero matrix. >> A=[3 3;2 1]; O=zeros(2,2); 4 4 Calculate A + 0. >> A+O ans = 3 2 Calculate A. >> A A = 3 2 3 1 Calculate B + A. >> B+A ans = 4 4 3 1 4 4 b) Enter the matrices >> A=[3 3;2 1]; B=[1 1;2 3]; >> C=[3 1;5 8]; Calculate (A + B)C. >> (A+B)+C ans = 7 5 9 12 Calculate AC + BC. d) Enter the matrix A and the zero matrix. startMatlab >> A=[3 3;2 1]; O=zeros(2,2); Calculate A + (? A). 100 Chapter 2 Vectors and Matrices in Matlab gt;> A+(-A) ans = 0 0 0 0 Calculate the zero matrix. >> O O = 0 0 0 0 5. >> (A+B)*C ans = 1 11 4 116 >> A*C+B*C ans = 1 11 4 116 a) Ener the matrices A and B. >> A=pascal(3); B=magic(3); Compute (A + B)T . 3. a) Enter the matrices A, B, and C. >> A=[1 0;2 5]; B=[0 1;2 7]; >> C=[1 2;0 9]; Compare A(B + C) and AB + AC. >> A*(B+C) ans = 1 3 12 86 >> A*B+A*C ans = 1 3 12 86 b) Enter the matrices A, B, and C. >> A=[1 0;2 5]; B=[0 1;2 7]; >> C=[1 2;0 9]; Compare (A+B)C and AC +BC. >> (A+B). ’ ans = 9 4 2 7 7 10 b) Compute AT + B T . >> A. ’+B. ’ ans = 9 4 2 7 7 10 12 8 5 12 8 The transpose of the sum of two matrices is equal to the sum of the transposes of the two matrices. 7. Enter matrix A and vector x. Section 2. 2 13. >> A=[1 1 2;3 4 0;0 5 6]; >> x=[1 2 5]. ’; Calculate Ax. >> A*x ans = 13 11 40 Matrices in Matlab 101 a) Enter the matrix A. >> A=pascal(5) To add a row of all ones to the bottom of the matrix, execute the following command. >> A(6,:)=ones(5,1) b) Enter the matrix A. >> A=pascal(5) To add a column of all ones to the right end of the matrix, execute the following command. >> A(:,6)=ones(5,1) 9. Enter matrices A and B. gt;> A=[2 3 1;0 1 2;0 0 5]; >> B=[1 1 4;0 0 5;3 5 2]; Calculate AB. >> A*B ans = 5 6 15 7 10 25 25 9 10 15. a) Enter the matrix A. 11. Enter matrix A. >> A=pascal(8); The following command will zero out all the odd columns. >> A(:,1:2:end)=0; >> A=ones(5); We’ll build a new matrix using the ? rst two roes of matrix A, then a row of 5’s, then the last three rows of matrix A. Note that we separate new columns with commas. 102 Chapter 2 Vectors and Matrices in Matlab >> B=[A(1:2,:);5*ones(1,5); A(3:5,:)] B = 1 1 1 1 1 1 1 1 1 1 5 5 5 5 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 b) Enter the matrix A. gt;> A=ones(5); We’ll build a new matrix using the ? rst 3 columns of matrix A, then a column of 5’s, then the last two columns of matrix A. Note that we separate new rows with semicolons. >> B=[A(:,1:3),5*ones(5,1), A(:,4:5)] B = 1 1 1 5 1 1 1 1 1 5 1 1 1 1 1 5 1 1 1 1 1 5 1 1 1 1 1 5 1 1 >> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 The next command will subtract 4 times row 1 from row 2. >> A(2,:)=A(2,:)-4*A(1,:) A = 1 2 3 0 -3 -6 7 8 9 b) Continuing with the last value of matrix A, the next command will subtract 7 times row 1 from row 3. >> A(3,:)=A(3,:)-7*A(1,:) A = 1 2 3 0 -3 -6 0 -6 -12 9. a) Enter the matrices A and B and compute C. >> A=magic(3); B=pascal(3); >> C=A+i*B The transpose of ? ? 8+i 1+i 6+i C = ? 3 + 1 5 + 2i 7 + 3i ? 4 + i 9 + 3i 2 + 6i 17. a) Enter the matrix A. Section 2. 2 is 8+i 3+i 4+i ? 1 + i 5 + 2i 9 + 3i ? . C = 6 + i 7 + 3i 2 + 6i T Matrices in Matlab 103 ? ? second dimension with the following command. You’ll note that the sum of each row is 260. >> sum(A,2) ans = 260 260 260 260 260 260 260 260 This result is veri? ed with the following Matlab command. >> C. ’ b) The conjugate ? 8+i ? 3 + 1 C= 4+i is 8? i 3? i 4? i ? 1 ? i 5 ? 2i 9 ? 3i ? . C = 6 ? 7 ? 3i 2 ? 6i T transpose of ? 1+i 6+i 5 + 2i 7 + 3i ? 9 + 3i 2 + 6i ? ? 23. Store A with the following command. >> A=pascal(5) A = 1 1 1 1 2 3 1 3 6 1 4 10 1 5 15 This result is veri? ed with the following Matlab command. >> C’ 1 4 10 20 35 1 5 15 35 70 21. Enter matrix A. >> A=magic(8) You sum the rows along the ? rst dimension with the following command. You’ll note that the sum of each column is 260. >> sum(A,1) You sum the columns along the You store I with the following command. >> I=eye(5) I = 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 Note that AI is identical to matrix A. 04 Chapter 2 Vectors and Matrices in Matlab You should be able to compute vwT manually and get the same result. >> A*I ans = 1 1 1 1 1 1 2 3 4 5 1 3 6 10 15 1 4 10 20 35 1 5 15 35 70 27. Load the matrices A and B. >> A=ones(2); B=2*ones(3); Load the matrix C. >> C=3*ones(2);You can construct the required matrix with the following command. >> D=[A,zeros(2,3),zeros(2,2); zeros(3,2), B, zeros(3,2); zeros(2,2), zeros(2,3), C] A 256? 256 identity matrix would have 1’s on its main diagonal and zeros in all other entries. 25. a) Store the vectors v and w. >> v=(1:5). ’; w=(2:6). ; The inner product vT w is computed as follows. >> v. ’*w ans = 70 You should be able to compute vT w manually and get the same result. b) The outer product vwT is computed as follows. >> v*w. ’ ans = 2 3 4 6 6 9 8 12 10 15 29. The entry in row 1 column 1 would be H(1, 1) = 1/(1 + 1 ? 1) = 1. The entry in row 1 column 2 would be H(1, 2) = 1/(1+2? 1) = 1/2. Continuing in this manner, we arrive at a 4 ? 4 Hilbert matrix. ? ? 1 1/2 1/3 1/4 ? 1/2 1/3 1/4 1/5 ? H=? ? 1/3 1/4 1/5 1/6 1/4 1/5 1/6 1/7 This result can be veri? ed by these commands. 4 8 12 16 20 5 10 15 20 25 6 12 18 24 30 >> format rat >> H=hilb(4)

Cite this page

Matrices in Matlab. (2019, Mar 19). Retrieved from https://paperap.com/paper-on-essay-matrices-in-matlab/

Matrices in Matlab
Let’s chat?  We're online 24/7