How to get the multiplication of elements in a row vector by using for loop

22 views (last 30 days)
% create a vector
A = [-2,4,9,-5,1];
Mult = 0;
% create a for loop
for i = 1:length(A)
Mult = Mult * A(i) ;
end
% display the result
disp(Mult);

Accepted Answer

KSSV
KSSV on 15 Mar 2022
% create a vector
A = [-2,4,9,-5,1];
Mult = 1; % <----- this should be 1
% create a for loop
for i = 1:length(A)
Mult = Mult * A(i) ;
end
% display the result
disp(Mult);
360
prod(A) % inbuit function
ans = 360
  4 Comments
Shuoze Xu
Shuoze Xu on 15 Mar 2022
Ok,bro.
That my first time to use that, i do not know i need to touch the accepting and voting.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!