Incrementally growing and adding to a matrix

13 visualizaciones (últimos 30 días)
Aryn
Aryn el 1 de Abr. de 2011
Hello,
I'm new to matlab and am having a lot of trouble figuring out how to add elements to a matrix while simultaneously increasing the value of the existing elements.
For example, if my increment of change is .2, then i would want each number in my matrix to grow by .2 during each timestep. However, I also need to be making the matrix one element larger at each timestep.
t1 = .2 t2 = .4 .2 t3 = .6 .4 .2
I'm kind of at a loss, so any help would be greatly appreciated!
etc, etc.

Respuestas (3)

Jan
Jan el 1 de Abr. de 2011
t = [];
for i=1:100
t = [t, 0] + 0.2; % Or [t + 0.2, 0.2];
end
Another method:
for i = 1:100;
t = (i:-1:1) * 0.2;
end
Be aware that a growing array is a very inefficient method. It is better to create the vector once and use indexing to operate on parts:
tFull = (100:-1:1) * 0.2;
for i = 1:100
t = tFull(101-i:100);
end

Sean de Wolski
Sean de Wolski el 1 de Abr. de 2011
Don't do this!!
t = 0.2:0.2:maxt
or; if you need what you have above
t = tril(toeplitz(0:.2:1));
t(3,:)
  1 comentario
Jan
Jan el 2 de Abr. de 2011
The FAQ concerns the formulation "t1=..., t2=...". As fas as I understand, the OP does not really want to create these different variables explicitely. If he does: Please follow Sean's link. +1

Iniciar sesión para comentar.


Aryn
Aryn el 1 de Abr. de 2011
I tried the first method and it worked. Luckily, I only have to go through 30 or 40 iterations. The second method might be a little complex for me at this point, but I will definitely look into it.
Thanks to both for the quick response!!!
  1 comentario
Sean de Wolski
Sean de Wolski el 1 de Abr. de 2011
If you give us an idea of the bigger picture we can probably help you refine the global approach as well as just creating t.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices 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