Multiply x sequentially with items of a vector

1 visualización (últimos 30 días)
Christoph Meier
Christoph Meier el 7 de Ag. de 2015
Comentada: Torsten el 7 de Ag. de 2015
Dear MATLAB community,
I would like to construct an index, which should start with 100. I have computed a vector, which then determines the change in the index in each time period. For example, for 4 time periods:[1.1,1.2,1.3,1.4] Basically, the operation should compute and produce the following vector:
  1. 100*1.1
  2. (100*1.1)*1.2
  3. ((100*1.1)*1.2)*1.3
  4. (((100*1.1)*1.2)*1.3)*1.4
Thank you very much in advance! I appreciate any help, as I am still a beginner with MATLAB.

Respuesta aceptada

Torsten
Torsten el 7 de Ag. de 2015
Editada: Torsten el 7 de Ag. de 2015
vder(1)=v(1);
for l=2:length(v)
vder(l)=vder(l-1)*v(l);
end
vder=100*vder;
v is the original vector, vder is the vector you are looking for.
Best wishes
Torsten.
  2 comentarios
Stephen23
Stephen23 el 7 de Ag. de 2015
Editada: Stephen23 el 7 de Ag. de 2015
See Walter Roberson's answer for the simpler and faster way to do this.
Torsten
Torsten el 7 de Ag. de 2015
And cumprod.m works without a loop ?
Best wishes
Torsten.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 7 de Ag. de 2015
Editada: Stephen23 el 7 de Ag. de 2015
cumprod([100, 1.1, 1.2, 1.3, 1.4])

Categorías

Más información sobre Matrix Indexing 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