Problem adding components of a vector using a while loop

조회 수: 3 (최근 30일)
Mary Jeppson
Mary Jeppson 2016년 5월 11일
댓글: Mary Jeppson 2016년 5월 12일
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일
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by