フィルターのクリア

how to find the sum of product of elements of an array in matlab

52 ビュー (過去 30 日間)
Chandradhar Savanth
Chandradhar Savanth 2013 年 10 月 3 日
コメント済み: Biruk Tadesse 2020 年 12 月 31 日
hi,
let p = [1 2 33 44 -11 -22] is single row multiple column matrix
i = index of elements of 'p',
in this case i = [1 2 3 4 5 6]
then x = sum(i*p(i)),i.e,'x' is equal to sum of product of corresponding elements of 'i' and 'p';
how to get 'x',
i know that we can compute 'x' by using loops.but is there any better way other than loops.

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 3 日
p = [1 2 33 44 -11 -22]
i = [1 2 3 4 5 6]
out=sum(i.*p(i))
  2 件のコメント
Chandradhar Savanth
Chandradhar Savanth 2013 年 10 月 3 日
Its working
thank you Azzi Abdelmalek ,
in the same above question can i find sum of product of only first 4 elements.
i.e,x = sum(i.*p(i)),where 'i' and 'p' takes first four elements of there respective arrays.
Regards,
Chandradhar Savanth.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 3 日
out=sum(i(1:4).*p(i(1:4)))

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

その他の回答 (1 件)

Debnarayan Bhattacharya
Debnarayan Bhattacharya 2019 年 10 月 1 日
編集済み: Debnarayan Bhattacharya 2019 年 10 月 1 日
Easier way is:
p = [1 2 33 44 -11 -22]
i = 1:size(p, 2)
out = p * i'
Alternately,
out = dot(p, i)
I am not aware of a function named sumproduct in MATLAB. However, the dot() serves the purpose.
  1 件のコメント
Biruk Tadesse
Biruk Tadesse 2020 年 12 月 31 日
any one help me p/se matlab cod for the equation

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by