how to multiply every element in an array?

조회 수: 19 (최근 30일)
Cem Kurukaya
Cem Kurukaya 2022년 4월 12일
댓글: Voss 2022년 4월 12일
o=[3,1,9];
This is an example of array. I want to multiply them each others. For example;
3x1x9 = 27
The lenght of array could be different. How can I calculate the multiplaction with for loop.

채택된 답변

Voss
Voss 2022년 4월 12일
o = [3 1 9];
% no for loop:
p = prod(o);
disp(p);
27
% some for loop:
p = 1;
for ii = 1:numel(o)
p = p*o(ii);
end
disp(p);
27
  댓글 수: 2
Cem Kurukaya
Cem Kurukaya 2022년 4월 12일
thanks a lot
Voss
Voss 2022년 4월 12일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Torsten
Torsten 2022년 4월 12일
편집: Torsten 2022년 4월 12일
product = prod(o)
is the short version,
product = 1.0;
for i = 1:numel(o)
product = product*o(i);
end
product
is the long version.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by