How can I calculate the product of elements in even positions in a vector using a for loop, without using sum or prod built-in functions?

11 ビュー (過去 30 日間)
I'm having trouble calculating the product of even positions in a given matrix using a for loop. The vector is 100,000 uniformly randomly generated numbers between 0 and 50, and I know that is uniform=(50)*rand(1,100000). I don't know how to get the product of the even positions using for loops, without using prod function. Any help would be appreciated

回答 (1 件)

Lessmann
Lessmann 2015 年 11 月 4 日
Hi,
you can use the modulo function to detect the even positions and then simply multiply the values.
z = (50)*rand(1,100000);
r = 0
for ii = 1:n
if ~mod(ii,2)
r = r*z(ii)
end
end
  4 件のコメント
EaglesFan
EaglesFan 2015 年 11 月 5 日
Sorry, you are correct. The product of the even positions of the vector are then divided by the sum of the odd positions. If help could be provided here as well that would be great.
Torsten
Torsten 2015 年 11 月 5 日
Expected value of the result is 25^100000/(100000*25).
Still much too large in magnitude.
Best wishes
Torsten.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by