Problem adding components of a vector using a while loop

1 次查看(过去 30 天)
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

采纳的回答

Adam
Adam 2016-5-11
编辑:Adam 2016-5-11
You should be using
b = b + x(k)
rather than
b = b + k(x)
x is your vector, k is the index into it.

更多回答(1 个)

Stalin Samuel
Stalin Samuel 2016-5-11
replace b = b+k(x) into b = b+x(k)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by