Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a MATLAB function that computes the product of two matrices; the function

ID: 3680675 • Letter: W

Question

Write a MATLAB function that computes the product of two matrices; the function header should be “function C = myM atrixP rod(A, B)”, where A and B are your matrices inputs, and C is the result matrix. Your function should include the following: (1) 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”. (2) 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 cij in C. Note: you should NOT directly use A B to do the multiplication.

Explanation / Answer

function [C] = myMatrixP (A, B)
% MyMatrixP function that computes
% the product of two matrices


% Your function should include the following:
% (1) 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”.
[m,n]=size(A);
[o,p]=size(B);
if (n~=o), error('Error: matrix dimensions not compatible'); end

% (2) 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 cij in C.
% Note: you should NOT directly use A B to do
% the multiplication.
C=zeros(m,p);
for (i=1:1:m)
for(j=1:1:p)
value=0;
for (k=1:1:n)
value=value+(A(i,k)*B(k,j));
end
C(i,j)=value;
end
end
end

%*********************************

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote