The idea here is to multiply matrix A with column B in a function file. The assignment is: "Make a function file of the following form:
function D = matmult(A,B)
that multiplies matrices A and B of arbitrary dimensions. Do this by using only the scalar multiplying operator `*'. In other words, write a matrix multiplication such that only (multiple times after each other) two normal numbers are multiplied." "Fill the resulting matrix, element after element, with a for-loop. The value of each element is a summation of different product terms (an inner-product). For this a new for-loop needs to be implemented in the first for-loop with the following assignment instruction:
sum = sum + change (the new value of the sum becomes the previous value of the sum plus the value of the change)
A possible example is given below >> A = [3 4;6 1]; >> B = [1 3]'; >> D = matmult(A,B) where the answer becomes D = [15 9]'
The code I made is almost there. The only bit I am confused about is how to improve the lines from 'sum = 0;' on.
function D = matmult(A,B)[a,b] = size(A);[c,d] = size(B);if b==c disp('match of matrix sizes'); D = zeros(a,d);else error('mismatch of matrix sizes');endsum = 0;for j = 1:d for i = 1:achange = A(i,j)'.*B(i,j);sum = sum + change; endend D = [sum sum]';
Best Answer