[Quick Question]Multiplications of all elements

Hi guys,
I have a matrix A = [ 1;9;1;4;4;6]
Is there any efficient way other than writing a loop that reach the result,
B = 1*9*1*4*4*6 = 864
Thanks a lot
==== Problem solved. Those comments are really useful!

 採用された回答

per isakson
per isakson 2012 年 8 月 18 日

1 投票

Yes. Try
B = prod( A )
B =
864

6 件のコメント

Jan
Jan 2012 年 8 月 18 日
編集済み: Jan 2012 年 8 月 18 日
You can find such solutions using the documentation, e.g. type lookfor product in the command line. Even asking Google for "Matlab multiply all elements" let you find a bunch of related links, which suggest to use PROD().
per isakson
per isakson 2012 年 8 月 18 日
Or you can ask at Answer ;-)
Xiao Tang
Xiao Tang 2012 年 8 月 18 日
編集済み: Xiao Tang 2012 年 8 月 18 日
Thanks, it's more than an answer!
However, I realized that what I really want is B(1) = A(1)=1, B(2) =A(1)*A(2)=9,B(3) = A(1)*A(2)*A(3)=9,B(4)= A(1)*A(2)*A(3)*A(4) = 36,etc. Any idea other than a loop like
for i =1:n; B(i) = prod(A(i)); end
Matt Fig
Matt Fig 2012 年 8 月 18 日
CUMPROD
Oleg Komarov
Oleg Komarov 2012 年 8 月 18 日
@Xiao: the documentation of prod() references cumprod(). You better give a try to the documentation, since it will definitely save you time.
Xiao Tang
Xiao Tang 2012 年 8 月 18 日
I see. Thanks a lot guys!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by