Write a MATLAB function that computes the product of two matrices; the function
ID: 1717009 • Letter: W
Question
Write a MATLAB function that computes the product of two matrices; the function header should be "function C = myMatrixProd(A, B)", where A and B are your matrices inputs, and C is the result matrix. Your function should include the following: Using if construction to check the compatibility requirement (i.e. A's column number should equal to B's row number). If not satisfied, stop the function and return an error message "Error: matrix dimensions not compatible". If the compatibility requirement is satisfied, using for loops to compute the inner product of row i of A and column j of B; and assign the result to the element c_ij in C. you should NOT directly use A * B to do the multiplication. Further, test your function ''myMatrixProd.m" for the following two cases: A_1=[-3 1 0], B_1 = [0 1 2 4 -2 7], and A_2 = [-3 1 0 9 8 -6],B_2 = [0 1 9 2 4 2 -2 7 -1].Explanation / Answer
function C =mymatrixprod(A,B)
[m1,n1]=size(A);
[m2,n2]=size(B);
if(n1~=m2)
error('matrix dimensions are not compatible');
else
C=zeros(m1,n2);
for x=1:m1
for y=1:n2
sum=0;
for z=1:n1
sum=sum+A(x,z).*B(z,y);
end
C(x,y)=sum;
end
end
end
%program
a=input('enter 1st matrix');
b=input('enter 2nd matrix');
product=mymatrixprod(a,b)
%result
>> m19
enter 1st matrix[-3 1 0]
enter 2nd matrix[0 1;2 4;-2 7]
product =
2 1
>> m19
enter 1st matrix[-3 1 0;9 8 -6]
enter 2nd matrix[0 1 9;2 4 2;-2 7 -1]
product =
2 1 -25
28 -1 103
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.