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

Looking for help on writing matlab files. here\'s the problem Recall that if A i

ID: 3561399 • Letter: L

Question

Looking for help on writing matlab files. here's the problem

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.
(a) 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
C = [A(1,:)*B; A(2,:)*B; A(3,:)*B]
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. Generate two random matrices A and B and compare the output of your
function file with the product A*B.
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 columns of the two matrix.
For instance, if A is 3 4 and B is 4 5, the product is given 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 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. Generate two random matrices A and B and compare the output of your
function file with the product A*B.
Include in your lab report the function M-file and the output obtained by running it

Explanation / Answer

function y =inmat(A,B) % This is a program designed to multiply two matrices % together by using the inner product. % % Written by Martha Allen % % Call syntax: inmat(A,B) % Input: an mxp matrix A and an pxn matrix B % Output: the product AB which is an mxn matrix C m= size(A,1); % gets the row size of matrix A p= size(A,2); % gets the column size of matrix A n= size(B,2); % gets the column size of matrix B f= size(B,1); % gets the row size of matrix B if f==p C=zeros(m,n); % initializes the variable C for i=1:m % starts the loop for j=1:n % starts the loop C(i,j)=C(i,j) + A(i,:) * B(:,j); % updates C end % ends loop end % ends loop ans = C % prints the final answer C else error('The dimensions of the matrices must agree.') end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote