Consider two matrices A (2 x 4) and B (4 x 3). Write a MATLAB script to calculat
ID: 3549804 • Letter: C
Question
Consider two matrices A (2x4) and B (4x3). Write a MATLAB script to calculate the product C = AB. Write your script in terms of matrix entries cij, aij and bij as described in class. Recall that each element in the product matrix is the dot product of the rows in the first matrix with the columns in the second matrix
where n is the inner dimension when forming the product AB. Note that the script should be general and thus should be able to perform the product for any A and B specified by the user. Show that your script works by computing the product AB for
A = [ 1 0 3 -1]
[ 2 1 0 2]
Do not use the MATLAB matrix multiplication operator (*).
B = [ 4 1 0 ]
[-1 1 3 ]
[ 2 0 1 ]
[ 1 3 4 ]
Sorry. I was unable to paste and/or attach the orginial image of the matrices.
Explanation / Answer
function c = product(A , B)
[rowsA, colsA] = size(A);
[rowsB, colsB] = size(B);
if colsA~=rowsB
disp("Matrix product not possible");
c = -1;
else
c = zeros(rowsA,colsB);
for i=1:rowsA
for j=1:colsB
for k=1:colsA
c(i,j) = c(i,j)+A(i,k)*B(k,j);
end
end
end
end
end
A = [ 1 0 3 -1; 2 1 0 2]
B = [ 4 1 0 ;-1 1 3;2 0 1;1 3 4]
product(A,B)