How to multiply two matrices with nested for loops ?

How can I do that with two matrices with any dimensions that can multiply ? Thank you

1 件のコメント

kale jabbar
kale jabbar 2018 年 2 月 11 日
編集済み: kale jabbar 2018 年 2 月 11 日
A=input
b=input
c=[ ]
R=size(A)
G=size(b)
n=R(1)
m=G(2)
for i=1:n
for j=1:m
c(i,j)=A(i,:)*b(:,j)
end
end
c

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2016 年 11 月 23 日

0 投票

A=rand(n,p);
B=rand(p,m);
C=zeros(n,m);
for i=1:n
for j=1:m
C(i,j)=0.0;
for k=1:p
C(i,j) = C(i,j)+A(i,k)*B(k,j);
end
end
end
Best wishes
Torsten.

2 件のコメント

Denememe
Denememe 2016 年 11 月 24 日
Thank you quick reply but i have an another problem. How can i multiply the intiger 5 with a matrix like [2 2 2 2; 2 2 2 2; 2 2 2 2] to get an answer like [10 10 10 10; 10 10 10 10; 10 10 10 10]
Jan
Jan 2016 年 11 月 24 日
編集済み: Jan 2016 年 11 月 24 日
@Denememe: Please accept the answer to show, that the problem is solved. Open a new thread for a new question, because the Comment section is a bad location to do this.
Did you try to ask in internet search engine at first? You would have found the same code very fast. So please try to solve your homework by your own at first.

サインインしてコメントする。

その他の回答 (2 件)

pallab
pallab 2025 年 1 月 9 日

0 投票

write a user define matrix multiplication function using if and for loop

1 件のコメント

Walter Roberson
Walter Roberson 2025 年 1 月 9 日

Well, you certainly can do that, but you have not explained how to do that, and so this answer is not at all useful in answering the question posed of how to write such code.

I admit to being rather unclear as to the benefit of using "if" statements in the matrix multiplication code? I can see using "if" in the initial tests to verify matrix size compatibility though.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2016 年 11 月 23 日

コメント済み:

2025 年 1 月 9 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by