Vector where elements equals previous elements + 1

3 次查看(过去 30 天)
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 个评论
Daniel Shub
Daniel Shub 2013-8-14
This is such a basic thing that I would really suggest you having another read of the manual.

请先登录,再进行评论。

采纳的回答

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

更多回答(2 个)

Daniel Shub
Daniel Shub 2013-8-14
Using end is a bad choice since it is special in MATLAB. I think what you are asking for is
a:b
  3 个评论

请先登录,再进行评论。


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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by