How to store values in an array from a loop
3 views (last 30 days)
Show older comments
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)
0 Comments
Answers (1)
dpb
on 20 Oct 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 Comments
dpb
on 20 Oct 2017
Edited: dpb
on 22 Oct 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... :)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!