Vector where elements equals previous elements + 1

4 views (last 30 days)
I wish to make a column vector where ecah element eqaul the previous element + 1. I also want to be able to decide by a loop how mane elements the vector should contain. Example: start =1;end=1 should give the vector [1}. start=1;end=3 should give the vector [1 2 3]. I hope someone has an idea about how to solve this.
  1 Comment
Daniel Shub
Daniel Shub on 14 Aug 2013
This is such a basic thing that I would really suggest you having another read of the manual.

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 14 Aug 2013
do as wrote Daniel
OR
end1 = 3;
for jj = end1:-1:1
vector(jj) = jj;
end
  2 Comments

Sign in to comment.

More Answers (2)

Daniel Shub
Daniel Shub on 14 Aug 2013
Using end is a bad choice since it is special in MATLAB. I think what you are asking for is
a:b
  3 Comments

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 14 Aug 2013
a=[1 1 1)
out=cumsum(a)
  4 Comments
Daniel Shub
Daniel Shub on 14 Aug 2013
Ahh now I see. I can see how the description would lead you to think CUMSUM is the right answer.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!