필터 지우기
필터 지우기

How to store values in an array from a loop

조회 수: 2 (최근 30일)
Nickolas Vrvilo
Nickolas Vrvilo 2017년 10월 20일
편집: dpb 2017년 10월 22일
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)

답변 (1개)

dpb
dpb 2017년 10월 20일
"...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
Stephen23
Stephen23 2017년 10월 20일
That is a rather misleading visual clue: nothing is being concatenated!
dpb
dpb 2017년 10월 20일
편집: dpb 2017년 10월 22일
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... :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by