Explicitly Multiplication of Matrices using For Loops

68 views (last 30 days)
Matman
Matman on 1 Oct 2016
Edited: Matman on 1 Oct 2016
I understand the concept of this, but only for when the matrices are the same size.
I now have Matrix A which is a 3x5 matrix and Matrix B which is a 5x4 matrix. I want to multiply the two explicitly using For loops. Can anyone help ?
Thanks in advance !
  2 Comments
Matman
Matman on 1 Oct 2016
I know that, the resulting matrix should be a 3x4.
This is the code that i have currently.
% Matrix Operations
% Implicit Calculations
A = [3 4 9 9 6;2 4 9 11 5;9 3 6 1 2];
B = [5 2 7 4 ; 6 11 4 9 ; 2 5 17 8 ; 9 11 18 9 ; 8 7 10 3];
C = A*B;
% Explicit Multiplications
% Matrix Multiplications
% For Matrix A
for ix = 1:3;
for jx = 1:5;
Cex(ix,jx)=0;
for kx = 1:5;
Cex(ix,jx) = Cex(ix,jx)+A(ix,kx);
end
end
end
% For Matrix B
for ix = 1:5;
for jx = 1:4;
Cex(ix,jx)=0;
for kx = 1:5;
Cex(ix,jx) = Cex(ix,jx)*B(kx,jx);
end
end
end
% Display the results
disp('C implicit = ')
disp(C)
disp('C explicit = ')
disp(Cex)
This is not currently working. I'm thinking that there's a basic error in there somewhere but not sure where. Be grateful if someone could help !

Sign in to comment.

Answers (2)

Andrei Bobrov
Andrei Bobrov on 1 Oct 2016
Edited: Andrei Bobrov on 1 Oct 2016
s1 = size(A);
s2 = size(B,2);
out=zeros(s1(1),s2);
for ii = 1:s1(1)
for jj = 1:s2
p=0;
for k = 1:s1(2)
p = p + A(ii,k)*B(k,jj);
end
out(ii,jj)=p;
end
end
  1 Comment
Matman
Matman on 1 Oct 2016
Thank you ! I'll test this out tomorrow.
If possible could you provide an explanation of what is going on in each step ?

Sign in to comment.


John D'Errico
John D'Errico on 1 Oct 2016
Edited: John D'Errico on 1 Oct 2016
Well, you do show some code. Lets see how we can write it to help you out. First, understand that a matrix multiply is just a set of dot products. So we can define the (ii,jj) element of the result as just
C(ii,jj) = dot(A(ii,:),B(:,jj))
Or, I could write it as:
C(ii,jj) = A(ii,:)*B(:,jj);
That too is a dot product, between the corresponding vectors.
Unfortunately, we can't just leave it like that, as MATLAB won't understand what we are writing. We need to put loops around it, defining ii and jj using for loops.
[ra,ca] = size(A);
[rb,cb] = size(B);
C = zeros(ra,cb);
for ii= 1:ra
for jj = 1:cb
C(ii,jj) = dot(A(ii,:),B(:,jj));
end
end
Note that I preallocated C. That was important.
Anyway, written as loops around a dot product, the above code will work. But really, this requires a triple set of loops, because the dot product itself can/should/might be expanded.
[ra,ca] = size(A);
[rb,cb] = size(B);
C = zeros(ra,cb);
for ii= 1:ra
for jj = 1:cb
% expanded: C(ii,jj) = dot(A(ii,:),B(:,jj));
for k = 1:ca
C(ii,jj) = C(ii,jj) + A(ii,kk)*B(kk,jj);
end
end
end
Note that ca and rb MUST be the same for the matrices in a matrix multiply to conform for multiplication.
The preallocation of C allows us to simply accumulate the results directly in place.
This final form is what the code would look like, if you saw it written in (very) old school fortran, etc.
  2 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!