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: 3790358 • Letter: R

Question

Recall that the multiplication of two matrices are defined as the following: [A]_min = [B]_mp[C] A_ij = sigma^p_k=1 B_ik C_kj With correct dimensions, MATLAB astir operator will give us the flexibility of doing these types of multiplications. Now, let us assume that this feature is not available in MATLAB. words, let us assume that MATLAB astir can be used only for multiplying two "scalar" values. 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 astir (i.e. write a program that does the job of the MAT. LAB operator) Compare the elapsed time that it takes to run this astir or astir program vs. simply using the or operators. Experiment with a variety of conditions (different matrix sizes, etc) and comment on the differences in efficiency This problem is worth 10 points (5 points for each part). Complete problems 5.31 through 5.33 in your textbook. This problem is worth 5 points.

Explanation / Answer

function [output_mat]= mat_mul(mat_A,mat_B)
[row_A,col_A]=size(mat_A);% Check row and columns of matrix A
[row_B,col_B]=size(mat_B); % Check row and columns of matrix B
output_mat=0;
if(col_A~=row_B) % Check the dimension
output_mat='ERROR (Input Matrix Dimension Error)';
return;
end
sum=0;
output_mat1=zeros(row_A,col_A);
for i=1:row_A
for j=1:col_B
for k=1:col_A
sum = sum + mat_A(i,k)*mat_B(k,j); % formula for multiplication
output_mat1(i,j)=sum; % assignment of the multiplication
end
sum = 0;
end
end
output_mat=output_mat1;
end

--------------------------------------------------------------------------------------

function [output_mat]= mat_mul_1(mat_A,mat_B)

[row_A,col_A]=size(mat_A);
[row_B,col_B]=size(mat_B);
if(row_A~=row_B && col_A~=col_B) % Check that the dimension of the both matrix
output_mat='ERROR (Matrix Dimensions are different)';
return;
end
output_mat1=zeros(row_A,col_A);
for i=1:row_A
for j=1:col_A
output_mat1(i,j)=mat_A(i,j)*mat_B(i,j); % Element wise multiplications
end
end
output_mat=output_mat1;
end

---------------------------test program------------------

clc;
clear all;
A=[1 2 3;4 5 6];
B=[1 2;4 9;10 11];
tic;
mat_mul(A,B)
time_taken=toc;
tic;
A*B
time_taken1=toc;
fprintf('Our Function taken %.5f sec and * operator take %.5f ',time_taken,time_taken1);


A=[1 2 3;4 5 6; 7 8 9];
B=[1 2 9;4 9 1;10 11 15];
tic;
mat_mul_1(A,B)
time_taken2=toc;
tic;
A.*B
time_taken3=toc;
fprintf('Our Function taken %.5f sec and .* operator take %.5f ',time_taken2,time_taken3);

----------------------Final Output--------------------------------------


ans =

39 53
84 119


ans =

39 53
84 119

Our Function taken 0.01163 sec and * operator take 0.00124

ans =

1 4 27
16 45 6
70 88 135


ans =

1 4 27
16 45 6
70 88 135

Our Function taken 0.01192 sec and .* operator take 0.00134
>>