MATLAB: How to find the sum of product of elements of an array in matlab

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

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.

Best Answer

p = [1 2 33 44 -11 -22]
i = [1 2 3 4 5 6]
out=sum(i.*p(i))