How to multiply matrices using for loops?
Afficher commentaires plus anciens
I have a problem in which I have to multiply two matrices, x (700x900) and y(900,1100), using a for loop. I'm not sure where to start, I've only been using MATLAB for about a month. Any advice/help/suggestions would be great!
Thank You
Réponse acceptée
Plus de réponses (1)
Kan Sun
le 22 Jan 2019
4 votes
Suppose you have matrix1(N*M) and matrix2(M*L), then you can have the product using for loops as following:
product=zeros(N,L);
for i=1:N
for j=1:L
product(i,j)=matrix1(i,1)*matrix2(1,j);
for k=2:M
product(i,j)=product(i,j)+matrix1(i,k)*matrix2(k,j);
end
end
end
product1=product
At last you can compare this product1 to the result of matrix1*matrix2 to see if they are the same (yes they are).
1 commentaire
Aditya Sur
le 6 Mar 2022
I did'nt understand this statement: product(i,j)=matrix1(i,1)*matrix2(1,j);
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!