An increasing length vector error to find the last component (made through a loop)

2 views (last 30 days)
Hello all,
I have created a vector "X" with an initial value for "X1", through a loop, I am adding some other components to this vector. So, at the end of loop: for q=1:n
I should have a vector of length (n+1) for sure. However, for example for n=20, for some runs I have it correct and the vector is made OK. But, some another times that I push the run button (for the same code), I get the following error:
Attempted to access x1(6); index out of bounds because numel(x1)=5. or Attempted to access x1(10); index out of bounds because numel(x1)=9. or....
Do any of you know why? I mean why the result of this code is not consistent?
Regards, Mahdi

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!