Problem adding components of a vector using a while 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

1 件のコメント

Mary Jeppson
Mary Jeppson 2016 年 5 月 12 日
Thank you Stalin Samuel and Adam. I appreciate the help.

サインインしてコメントする。

 採用された回答

Adam
Adam 2016 年 5 月 11 日
編集済み: Adam 2016 年 5 月 11 日

0 投票

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 日

0 投票

replace b = b+k(x) into b = b+x(k)

カテゴリ

質問済み:

2016 年 5 月 11 日

コメント済み:

2016 年 5 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by