Write a function with the header: function [C] = myMatMat (A, B) that implements
ID: 3730545 • Letter: W
Question
Write a function with the header: function [C] = myMatMat (A, B) that implements nested for-loops to multiply A and B and assigns the result to C. If A and B are not compatible, myMatMat should return -1 for C.
Problem 1: (5 Points) Write a function with the header: function [C] = myMatMat (A, B) that implements nested for-loops to multiply A and B and assigns the result to C If A and B are not compatible, myMatMat should return -1 for c 23 34 >>A=[35 39 41 41 18 39 34 41 12 21 28 39 27 41 30 14 >> B=[2 17 28 29 30 2 2 32 32 36 19 17 34 2 12 35 CmyMatMat (A, B) 2258 1794 2208 1890 2890 3126 1900 2839 2635 2062 2603 3183 2769 1916 1504 1324 478 1652 1300 724 738 1375 493 847 1478 1449 1650 1109 2732 1984 1474 2828 2744 2364 1774 1511 1859 1394 1085 959 1097 874 1411 1486 899 2209 2729 1736 1837 1879 1561 1016 2557 1887 1334Explanation / Answer
function [C] = myMatMat (A, B)
[ar, ac] = size(A);
[br, bc] = size(B);
if( ac ~= br)%Doesn't match, can't mulitply
C = -1;
return
end
C = zeros(ar, bc);
for i = 1:ar
for j = 1:bc
for k = 1:ac
C(i, j) = C(i, j) + A(i, k) * B(k, j);%for each cell in A nd B multiply and add to C matrix
end
end
end
end
Sample Output -
>> A = [1 2 3; 4 5 6; 7 8 9];
>> B = [1 2 3];
>> A*B
Error using *
Inner matrix dimensions must agree.
>> A*B'
ans =
14
32
50
>> myMatMat(A, B)
ans =
-1
>> myMatMat(A, B')
ans =
14
32
50
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.