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

조회 수: 5 (최근 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