MATLAB: Problem adding components of a vector using a while loop

summing vectorswhile loop

Can anyone tell me what's wrong with this code?
x = [1 23 43 72 87 56 98 33];
b = 0;
k = 1;
while k<=length(x)
b = b+k(x)
k = k+1
end
I get index exceeds matrix dimensions. Thank you very much for your help

Best Answer

You should be using
b = b + x(k)
rather than
b = b + k(x)
x is your vector, k is the index into it.
Related Question