5. Recall that if A is an m x n matrix and B is a p × q matrix, then the product
ID: 2260373 • Letter: 5
Question
5. Recall that if A is an m x 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 mx 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 × 4 and B is 4 × 5, the product is given by the matrix 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 × 2 matrix B . Compare the output with A*B Repeat with 3 x 4 and 4 × 2 matrices and with 3 × 4 and 2 × 4 matrices. Include in your lab report the function M-file and the output obtained by running it (b) Write a function M-file that takes as input two matrices A and B, and as output produces the product by rows of the two matrices. For instance, if A is 3 × 4 and B is 4 × 5, the product/AB is given by the matrix 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 rows of C). Call the file rowproduct.m Test your function on a random 2 × 3 matrix A and a random 3 × 2 matrix B . Compare the output with A*B Repeat with 3 × 4 and 4 × 2 matrices and with 3 × 4 and 2 × 4 matrices. Include in your lab report the function M-file and the output obtained by running it 2016 Stefania Tracogna, SoMSS, ASUExplanation / Answer
5a)
%%%% Matlab function
function [c] = matpro (a,b)
[m1,n1]=size(a);
[m2,n2]=size(b);
if(n1==m2)
for n=1:n2
c(:,n)=a*b(:,n);
end
else
disp('Matrix dimension mismatch');
end
end
%%% test progromm
a=[1 2 3;2 3 4];
b=[1 2; 3 4;5 6];
matpro(A,b)
OUTPUT:
ans =
22 28
31 40
5b)
%%% Matlab function
function [c] = matpro (a,b)
[m1,n1]=size(a);
[m2,n2]=size(b);
if(n1==m2)
for n=1:m1
c(n,:)=a(n,:)*b;
end
else
disp('Matrix dimension mismatch');
end
end
%%% test programme
a=[1 2 3;2 3 4];
b=[1 2; 3 4;5 6];
matpro(a,b)
OUTPUT:
ans =
22 28
31 40
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.