MATLAB: How many number of values when added becomes equal to B(Known value)

sum of x numbers

I have to find the X value for which \sum_(i=1)^X (i * a_i ) >= B
or ( 1*a_1 + 2*a_2 + --- + X*a_x >= B ) where a_i & B are known.
For finding the X value; is there any explicit formula? If anyone knows, please answer.
Thank you very much, kalpana

Best Answer

Assume 'a' is a row vector.
x = 1:size(a,2);
X = x(cumsum(x.*a)>=B);