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

10 次查看(过去 30 天)
% 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);

采纳的回答

KSSV
KSSV 2022-3-15
% 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 个评论
Shuoze Xu
Shuoze Xu 2022-3-15
Ok,bro.
That my first time to use that, i do not know i need to touch the accepting and voting.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by