How to improve/debug my matrix-column multiplier?

1 view (last 30 days)
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');
end
sum = 0;
for j = 1:d
for i = 1:a
change = A(i,j)'.*B(i,j);
sum = sum + change;
end
end
D = [sum sum]';
  2 Comments
James Tursa
James Tursa on 29 Jun 2015
Edited: James Tursa on 29 Jun 2015
1) Don't use "sum" as a variable name since that is the name of an inbuilt MATLAB function. E.g., use "mysum" or some other name instead.
2) It isn't clear from your description what the result should be. Is this a standard matrix multiply, but you are coding it by hand using loops? Or is this some other type of calculation (if so, please provide a small example of input & output)? Once we know what the calculation is supposed to be, we can help. Thanks for posting the code you have so far. If you are coding a standard matrix multiply, you can see the formula for each element of the result here:
https://en.wikipedia.org/wiki/Matrix_multiplication
Look under the section "Matrix product (two matrices)" and the formula for (AB)ij and then code up the sum on the right hand side.
Titus Edelhofer
Titus Edelhofer on 29 Jun 2015
Some more comments:
  • I'm pretty sure it should be B(j,i) instead of B(i,j);
  • After the loop on i I guess it should be
D(i,j) = sum;
where sum should get another name as James already mentioned.
Regarding the performance: since you are specifically asked to do it element by element, in the end the performance will be rather slow compared to
D = A*B;
that's for sure ;-).
Titus
PS: BTW, if the assignment asks you to use "sum", then use James comment as an advice for your own code only ...

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 29 Jun 2015
Edited: James Tursa on 29 Jun 2015
Your example is a standard matrix multiply, and your attempt is not too far off, so I will provide some corrections with some other comments where you will need to complete the code (assuming your instructor wants to use a loop for the element sum as seems to be indicated from your description):
D = zeros(a,d); % Pre-allocate the result
for j = 1:d
for i = 1:a
sum = 0; % Move this line inside the for loops, each element sum starts at 0
for k=1:b % The "sigma" loop as shown in the Wiki link above
change = CODE FOR YOU TO FILL IN GOES HERE; % See Wiki link
sum = sum + change; % Incrementally sum up the changes for the (i,j) element
end
D(i,j) = sum; % Store the result for the (i,j) element
end
end
Pay particular attention to the indexing used for the "sigma" loop in the Wiki link.
https://en.wikipedia.org/wiki/Matrix_multiplication

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!