Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i've tried the code that's written within the function, please help Write a function MatMul to calculate the product of two Matrices if they are
i've tried the code that's written within the function, please help
Write a function MatMul to calculate the product of two Matrices if they are of compatible sizes(number of columns of matrixA and number of rows of matrixB are equal) using nested for loops or return empty matrix with O rows and 0 columns if they are not compatible sizes. (For primer see Khan Academy: Matrix multiplication). The two input arguments of MatMul are: matrixA: 2D numerical Array of size axb matrixB: 2D numerical Array of size cxd The output of MatMul is productAB of size axd if b=c, otherwise Oxo matrix. Hint: Use if else statements to check for matrix compatibilty and nested for loops to calculate the product for each row with each column. Restrictions: 1) The function must test of multiplication can be performed, if not, the function should return a empty matrix for productAB. Must use nested for loops, do not just multiply MatA and MatB. For example: Example 1: matrixA = [7,8,9;4,5,6] matrixB = [1,2;3,4,5,6] productAB = Matmul(matrixA, matrixB) Produces productAB = [76,100:49,64] As size of matrixA = 2x3 and matrixB = 3x2 they are of compatible sizes as (number of columns of matrixA =number of rows of matrixB) and can be multiplied, thus the output is of size 2x2 Example 2 matrixA = [7,8,9;4,5,6] matrixB = [1,2,3,4,5,6] productAB - Matmul(matrixA, matrixB) Produces productAB = [] As size of matrixA = 2x3 and matrixB = 2x3 they are not of compatible sizes as (number of columns of matrixA != number of rows of matrixB) and cannot be multiplied, thus the output is of size Oxo. function productAB = Matmul(matrixA, matrixB) %insert code if...%complete code for .......%complete code for .....%complete code %complete code end end else %complete code end end function productAB = Matmul(A,B) size_A = size(A); size_B = size(); rows_A = size_A(1); cols_A - size_A(2); rows_B = size_B(1); cols_B = size_B(2); C=[]; if cols_A = rows_B productAB = []; else C=zeros(rows_A, cols_B); for i=1:rows_A for j=1: cols_B C(i, j)=0.8; for k=1: cols_A C(i,j) = C(1,3)+A(i,k)*B(k,j); end end end end productAB - C; endStep 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