***MATLAB***Recall that if A is an m × n matrix and B is a p × q matrix, then th
ID: 3787077 • Letter: #
Question
***MATLAB***Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p, in which case C is an m × q matrix
5. Recall that if A is an m x n matrix and B is a p x q matrix, then the product C AB is defined if and only if n F p, in which case C is an m x q matrix. (a) Write a function M-file that takes as input two matrices A and B, and as output produces the product by columns of the two matrix. For instance, if A is 3 x 4 and B is 4 x 5, the product is given by the matrix c CA*B ,1), A B 2), A BC: 3) A BC: ,4), A BC: ,5)] The function file should work for any dimension of A and B and it should perform a check to see if the dimensions match (Hint: use a for loop to define the columns of C). Call the file columnproduct.m. Test your function on a random 2 x 3 matrix A and a random 3 x 2 matrix B. Compare the output with A B. Repeat with 3 x 4 and 4 x 2 matrices and with 3 x 4 and 2 x 4 matrices. Include in your lab report the function M-file and the output obtained by running itExplanation / Answer
function C = matrixmult(A,B)
[n,m] = size(A);
[p,q] = size(B);
if p~=m
error('Matrix cant be multiplied as inner dimension should be same.')
end
C = zeros(n,q);
for k = 1:q
C(:,k) = A*B(:,k)
end
end
A = [1,2,3,4;4,5,6,7;8,9,10,11];
B = [1,2,3;4,5,6;7,8,9;1,2,3]
C = matrixmult(A,B)
D = A*B
if (C ==D)
disp("OK")
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.