How to multiply matrices using for loops?
Mostra commenti meno recenti
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
Risposta accettata
Più risposte (1)
Kan Sun
il 22 Gen 2019
4 voti
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 Commento
Aditya Sur
il 6 Mar 2022
I did'nt understand this statement: product(i,j)=matrix1(i,1)*matrix2(1,j);
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!