How to multiply two matrices with nested for loops ?

39 views (last 30 days)
How can I do that with two matrices with any dimensions that can multiply ? Thank you
  1 Comment
kale jabbar
kale jabbar on 11 Feb 2018
Edited: kale jabbar on 11 Feb 2018
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

Sign in to comment.

Accepted Answer

Torsten
Torsten on 23 Nov 2016
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 Comments
Denememe
Denememe on 24 Nov 2016
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 on 24 Nov 2016
Edited: Jan on 24 Nov 2016
@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.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 23 Nov 2016

Categories

Find more on Matrices and Arrays 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!