Vector where elements equals previous elements + 1

3 visualizaciones (últimos 30 días)
Karl
Karl el 14 de Ag. de 2013
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 comentario
Daniel Shub
Daniel Shub el 14 de Ag. de 2013
This is such a basic thing that I would really suggest you having another read of the manual.

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 14 de Ag. de 2013
do as wrote Daniel
OR
end1 = 3;
for jj = end1:-1:1
vector(jj) = jj;
end
  2 comentarios
Daniel Shub
Daniel Shub el 14 de Ag. de 2013
I am not convinced Karl will be able to figure out make that go from A to B ...
Karl
Karl el 15 de Ag. de 2013
Thanks!

Iniciar sesión para comentar.

Más respuestas (2)

Daniel Shub
Daniel Shub el 14 de Ag. de 2013
Using end is a bad choice since it is special in MATLAB. I think what you are asking for is
a:b
  3 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 14 de Ag. de 2013
Why this is different from Daniel's answer?
n=5
a=1
b=n
out=a:b
Karl
Karl el 15 de Ag. de 2013
That did the trick. Thanks!

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 14 de Ag. de 2013
a=[1 1 1)
out=cumsum(a)
  4 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 14 de Ag. de 2013
I've just misunderstood the question
Daniel Shub
Daniel Shub el 14 de Ag. de 2013
Ahh now I see. I can see how the description would lead you to think CUMSUM is the right answer.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by