how to multiply every element in an array?

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 日

1 投票

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 日

1 投票

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.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 4 月 12 日

コメント済み:

2022 年 4 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by