Vector where elements equals previous elements + 1

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

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일

0 개 추천

do as wrote Daniel
OR
end1 = 3;
for jj = end1:-1:1
vector(jj) = jj;
end

댓글 수: 2

I am not convinced Karl will be able to figure out make that go from A to B ...
Karl
Karl 2013년 8월 15일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Daniel Shub
Daniel Shub 2013년 8월 14일

3 개 추천

Using end is a bad choice since it is special in MATLAB. I think what you are asking for is
a:b

댓글 수: 3

Karl
Karl 2013년 8월 14일
It is no problem for me using a term other than end. I just with to make a vector equall to [1 (1+1) (1+1+1) (1+1+1+1)...(1xn)] where I can adjust n...
Why this is different from Daniel's answer?
n=5
a=1
b=n
out=a:b
Karl
Karl 2013년 8월 15일
That did the trick. Thanks!

댓글을 달려면 로그인하십시오.

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 14일

0 개 추천

a=[1 1 1)
out=cumsum(a)

댓글 수: 4

Karl
Karl 2013년 8월 14일
This was an example where start =1 and end=3 in mye exemple aboove. However, I need a loop, so that I don't have to write the number of 1's every time as in your line 1. I have lots of different vetor lengths that I vary all the time.
Azzi, while I agree this works, why would you go this way?
I've just misunderstood the question
Ahh now I see. I can see how the description would lead you to think CUMSUM is the right answer.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by