this is the dot product of row i of A with column j of B. A=[1 2 3; 4 5 6; 7 8 9
ID: 3544284 • Letter: T
Question
this is the dot product of row i of A with column j of B.
A=[1 2 3; 4 5 6; 7 8 9; 10 11 12]
B=[2 1 6 5 10; 1 7 11 18 3; 20 3 5 9 2] to equal
C=[64 24 43 68 22; 133 57 109 164 67; 202 90 175 260 112; 271 123 241 356 157]
since each element of C is a dot product, you will need three levels of for loops( with three loop control variables):
-an outler loop for each row in C
-an intermediate loop for each column of C
-an intermost loop to perform the dot product of row i of A with column j of B
Write a user defined function called MatrixProduct which multiplies two compatible matrices.
a.) the function whould take two input arguments: A and B
b.)the function have one output argument: C
c.) the function should check to make sure A and B are compatible. if they are not, the function should assign "-1" to the output matrix C
d.)use i,j,k and k as the loop control variables for the outer, intermediate and innermost loops
Explanation / Answer
function C = MatrixProduct(A, B)
[rowA colA] = size(A);
[rowB colB] = size(B);
if rowB~=colA
disp ('*** matrices dimensions must match... ***');
C = -1;
else
C = zeros(rowA,colB);
for i=1:rowA
for j=1:colB
for k=1:colA
C(i,j) = C(i,j) + A(i,k)*B(k,j);
end
end % end inner for
end % end outer for
end % end if
end % end function
% uncomment below code to test above function.
%A = [1 2 3;4 5 6; 7 8 9; 10 11 12]
%B = [2 1 6 5 10;1 7 11 18 3;20 3 5 9 2]
%C = MatrixProduct(A,B)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.