Explicitly Multiplication of Matrices using For Loops
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
dpb
el 1 de Oct. de 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]
Respuestas (3)
Andrei Bobrov
el 1 de Oct. de 2016
Editada: Andrei Bobrov
el 1 de Oct. de 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 comentario
John D'Errico
el 1 de Oct. de 2016
Editada: John D'Errico
el 1 de Oct. de 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 comentarios
Islammuddin
el 15 de En. de 2025
Editada: Walter Roberson
el 15 de En. de 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 comentario
Steven Lord
el 15 de En. de 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.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!