Explicitly Multiplication of Matrices using For Loops

10 views (last 30 days)
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
dpb
dpb on 1 Oct 2016
Look up the computation in basic linear algebra textbook if that's the issue...the result of any matrix multiplication size is
[m x n] * [n x p] --> [m x p]
Matman
Matman on 1 Oct 2016
Edited: 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 (3)

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
Edited: 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
Matman
Matman on 1 Oct 2016
Edited: Matman on 1 Oct 2016
Thank you so much for the response. So using the last code you show in Matlab would allow me to output my solution ?
Apologies that my Matlab coding isn't exactly upto scratch. Could you please provide information as to what is going on in each step ?

Sign in to comment.


Islammuddin
Islammuddin on 15 Jan 2025
Edited: Walter Roberson on 15 Jan 2025
a=[3,2,3;6,5,4;1,2,3];
b=[5,7,4;8,6,2;7,8,9];
m=a+b;
c= a-b;
d= a*b;
using for loops and verify your answer by using built - in function
  1 Comment
Steven Lord
Steven Lord on 15 Jan 2025
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!