Answered step by step
Verified Expert Solution
Question
1 Approved Answer
43. Write your own code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same Every element in
43. Write your own code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same Every element in the resulting matrix Cis obtained by: 1 Cij aikDkj So, three nested loops are required Your Function 1 function [ mat3] - mymatmult( mat1, mat2) 2 3 4. [m,n]- size( matl); [k,1] - size( mat2); 8 9 10 mat3=[]; disp('Error, can' 't multiply the matrices'); return end mat3 - zeros(m,1); 12 13 for i-1m 14 15 16 17 18 19 20 end 21 end for j- 1:1 for k 1 : n mat3(i,j)- mat3(i,j)+ mat1(i , k) * mat2(k , j); end end V Test 8 (Pretest) Return error when inner dimensions don't agree Test Code: 1 %Test for error statement 2 errtest-0; 3 try 4 mymatmult(1 2 31,1 2 31) 5 catch err if isempty(err. identifier) errtest errtest + 1; 6 %not a MATLAB-generated error statement 7 8 end 9 end 10 try 11 mymatmult([1; 2; 31,[1; 2; 3]); 12 catch err 13 if isempty (err. identifier) %not a MATLAB-generated error statement 14 15 16 end 17 assert (errtest--2, 'Return error when inner dimensions don' 't agree') 18 19 errtest-errtest + 1; end Output: Error, can't multiply the matrices Error, can't multiply the matrices
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started