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

Recall that the multiplication of two matrices are defined as the following: [A]

ID: 3790480 • Letter: R

Question

Recall that the multiplication of two matrices are defined as the following: [A]_mn = [B]_mp [C]_pm doubleheadarrow A_ij = sigma^P_k = 1 B_ik C_kj With correct dimensions, MATLAB operator will give us the flexibility of doing these types of multiplications. Now, let us assume that this feature is not available in MATLAB. In other words, let us assume that MATLAB can be used only for multiplying two "scalar" values. Use for loop and write a program that receives two matrices as inputs and returns the product of them as its output. If the dimensions of the matrices do not match according to the multiplication rule, the function should output an error message. Repeat part (a) for MATLAB (i.e. write a program that does the job of the MATLAB operator). Compare the elapsed time that it takes to run this program vs. simply using the or operators. Experiment with a variety of conditions (different matrix sizes, etc) and comment on the differences in efficiency. Complete problems 5.31 through in your textbook.

Explanation / Answer

function C = matMult(A, B)

[n,m] = size(A);

[p,q] = size(B);

C = zeros(n,q);

  

if p~=m

error('Inner Matrix Dimensions Must Agree.')

end

  

for k = 1:n

for j = 1:q

temp=0;

for i = 1:p

temp = temp+(A(k,i)*B(i,j));

end

C(k,j) = temp;

end

end

end

A = [1,2,3;3,4,5;5,6,7];

B = [10,11,12;13,14,15;16,17,18];

C = matMult(A,B);

CDirect = A*B;