How to multiply matrices using for loops?
古いコメントを表示
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
採用された回答
その他の回答 (1 件)
Kan Sun
2019 年 1 月 22 日
4 投票
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 件のコメント
Aditya Sur
2022 年 3 月 6 日
I did'nt understand this statement: product(i,j)=matrix1(i,1)*matrix2(1,j);
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!