Borrar filtros
Borrar filtros

How to store values in an array from a loop

2 visualizaciones (últimos 30 días)
Nickolas Vrvilo
Nickolas Vrvilo el 20 de Oct. de 2017
Editada: dpb el 22 de Oct. de 2017
I am trying to store the values of i after each iteration into the tseArray. For example, I am trying to store the first value, 730374, into the array, and then the next value, (730374+543.4) into the next value. I then would like to be able to display the values of tseArray after the loop. There should be 150 iterations between my starting value and ending value, hence the zeros array for allocation purposes. However, I am having some trouble as I can't seem to figure out how to add and create values to append to that array. All help is appreciated, thank you.
tseArray = zeros(1,150);
for i = 730374:543:811824
date(i) = tseArray;
tseArray = date(i);
end;
disp(tseArray)

Respuestas (1)

dpb
dpb el 20 de Oct. de 2017
"...store first value, 730374, into the array, and then the next value, (730374+543.4) into the next value."
tseArray=[730374:543:811824];
But, the next value is not 543.4 difference from first; it's 543 days precisely.
If the 0.4 isn't a typo, then need to define what it is you're actually wanting for certain.
It is much "more better" to build date vectors using the increment of whatever time interval you want as integer to avoid potential floating point roundoff such that the resulting dates will be found reliably using native time functions.
Also, date numbers have been deprecated; use the new datetime class object instead.
  4 comentarios
Stephen23
Stephen23 el 20 de Oct. de 2017
That is a rather misleading visual clue: nothing is being concatenated!
dpb
dpb el 20 de Oct. de 2017
Editada: dpb el 22 de Oct. de 2017
Unnecessary, yes. "Misleading?" HOW???
The list inside the [] is concatenated (with itself if nothing else). :)
Again, it's a personal preference of nearly 30 yr practice; don't suspect I'll change it any time soon... :) The RHS just looks lonely to me w/o the comfort of the braces around it...
ADDENDUM
Could use () rather than [], but that's a case of the fingers are already trained so it's automatic when typing; would have to go back and re-edit virtually every time if were to try to teach the old dog... :)

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