4. The product y = Ax of an m n matrix A times a vector...

70.2K

Verified Solution

Question

Advance Math

4. The product y = Ax of an m n matrix A times a vector x = (x1;x2; : : : ; xn)T can be computed row-wise as y = [A(1,:)*x;A(2,:)*x; ... ;A(m,:)*x]; that is y(1) = A(1,:)*x y(2) = A(2,:)*x... y(m) = A(m,:)*x Write a function M-file that takes as input amatrix A and a vector x, and as output gives the product y = Ax byrow, as denoted above (Hint: use a for loop to denote each entry ofthe vector y.) Your M-file should perform a check on the dimensionsof the input variables A and x and return a message if thedimensions do not match. Call the file myrowproduct.m. Note thatthis le will NOT be the same as the myproduct.m example. Test yourfunction on a random 5x2 matrix A and a random 2x1 vector x.Compare the output with A*x. Repeat with a 3x5 matrix and a 5x1vector and with a 3x5 matrix and a 1x5 vector. Use the command randto generate the random matrices for testing. Include in your labreport the function M-file and the output obtained by runningit.

5. Recall that if A is an m n matrix and B is a p q matrix, thenthe product C = AB is denoted if and only if n = p, in which case Cis an m q matrix.

(a) Write a function M-file that takes as input two matrices Aand B, and as output produces the product by columns of the twomatrix. For instance, if A is 3x4 and B is 4x5, the product isgiven by the matrix C = [A*B(:,1), A*B(:,2), A*B(:,3), A*B(:,4),A*B(:,5)]The function file should work for any dimension of A and Band it should perform a
check to see if the dimensions match (Hint: use a for loop todenote each column of C). Call the file columnproduct.m.Test yourfunction on a random 5x3 matrix A and a random 3x5 matrix B .Compare the output with A*B. Repeat with 3 x 6 and 6 x 4 matricesand with 3 x 6 and 4 x 6 matrices.Use the command rand to generatethe random matrices for testing. Include in your lab report thefunction M-file and the output obtained by running it.

(b) Write a function M- file that takes as input two matrices Aand B, and as output produces the product by rows of the twomatrices. For instance, if A is 3 x 4 and B is 4x5, the product ABis given by the matrix C = [A(1,:)*B; A(2,:)*B; A(3,:)*B] Thefunction file should work for any dimension of A and B and itshould perform a check to see if the dimensions match (Hint: use afor loop to denote each row of C). Call the file rowproduct.m. Testyour function on a random 5x3 matrix A and a random 3x5 matrix B .Compare the output with A*B. Repeat with 3 x 6 and 6 x 4 matricesand with 3 x 6 and 4 x 6 matrices. Use the command rand to generatethe random matrices for testing.
Include in your lab report the function M-file and the outputobtained by running it.

Answer & Explanation Solved by verified expert
3.8 Ratings (440 Votes)
4 Required Matlab code with explanatory comments is givenbelowfunction    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students