Filter löschen
Filter löschen

Problem adding components of a vector using a while loop

1 Ansicht (letzte 30 Tage)
Mary Jeppson
Mary Jeppson am 11 Mai 2016
Kommentiert: Mary Jeppson am 12 Mai 2016
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

Akzeptierte Antwort

Adam
Adam am 11 Mai 2016
Bearbeitet: Adam am 11 Mai 2016
You should be using
b = b + x(k)
rather than
b = b + k(x)
x is your vector, k is the index into it.

Weitere Antworten (1)

Stalin Samuel
Stalin Samuel am 11 Mai 2016
replace b = b+k(x) into b = b+x(k)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by