How to multiply matrices using for loops?
Show older comments
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
Accepted Answer
More Answers (1)
Kan Sun
on 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 Comment
Aditya Sur
on 6 Mar 2022
I did'nt understand this statement: product(i,j)=matrix1(i,1)*matrix2(1,j);
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!