how to find the sum of product of elements of an array in matlab
52 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chandradhar Savanth
am 3 Okt. 2013
Kommentiert: Biruk Tadesse
am 31 Dez. 2020
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.
0 Kommentare
Akzeptierte Antwort
Azzi Abdelmalek
am 3 Okt. 2013
p = [1 2 33 44 -11 -22]
i = [1 2 3 4 5 6]
out=sum(i.*p(i))
2 Kommentare
Weitere Antworten (1)
Debnarayan Bhattacharya
am 1 Okt. 2019
Bearbeitet: Debnarayan Bhattacharya
am 1 Okt. 2019
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 Kommentar
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!