Increment dates using for loop

조회 수: 13 (최근 30일)
CM
CM 2019년 2월 9일
답변: Stephen23 2019년 2월 9일
I'm trying to fill a variable sized datetime array. I then want the declared datetime value to fill the first 3 elements before incrementing by one to fill the next three.This process repeats until the desired sized element is reach. See current output & desired output.
Code:
samples = input("Number of Samples to Generate: ");
DateBuilt = repmat(datetime(0,0,0),samples,1);
t = datetime(2018,1,1);
DateBuilt(1) = t
for i=1:samples
if i <= 3
DateBuilt(i) = t;
else
t = t + 1;
DateBuilt(i) = t;
end
Current Output:
Number of Samples to Generate: 14
DateBuilt =
14×1 datetime array
01-Jan-2018
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
30-Nov--0001
Desired Output:
Number of Samples to Generate: 14
DateBuilt =
14×1 datetime array
01-Jan-2018
01-Jan-2018
01-Jan-2018
02-Jan-2018
02-Jan-2018
02-Jan-2018
03-Jan-2018
03-Jan-2018
03-Jan-2018
04-Jan-2018
04-Jan-2018
04-Jan-2018
05-Jan-2018
05-Jan-2018
  댓글 수: 5
madhan ravi
madhan ravi 2019년 2월 9일
편집: madhan ravi 2019년 2월 9일
Why in your desired output the last date has only repeated twice , any reason for it?
CM
CM 2019년 2월 9일
Yes. The user input (*see “samples“ variable in code) determines the number of elements in the datetime array. The first element of array is filled with a datetime. The loop then continues to fill the 2nd and 3rd elements before incrementing by 1 day. the loop then proceeds to fill the 4th, 5th, and 6th elements before incrementing again and repeating the process for the next three elements This continues until the user defined size is reached. Repeating the last date only twice is my attempt to show that the loop stops based on an arbitrary number entered by the user.

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

채택된 답변

Eric Sofen
Eric Sofen 2019년 2월 9일
Are there any other calculations being done in the loop body of your actual code? If not, this can be done without looping:
samples = input("Number of Samples to Generate: ");
days = floor((0:samples-1)./3)'+1;
dt = datetime(2018,1,days);
If you do really need to loop for some other reason, there are things that need to change in your code:
1) It's missing and end to the conditional block.
2) t = t+1 ends up incrementing the day every iteration.
samples = input("Number of Samples to Generate: ");
DateBuilt = repmat(datetime(0,0,0),samples,1);
t = datetime(2018,1,1);
DateBuilt(1) = t;
for i=1:samples
DateBuilt(i) = t+days(floor((i-1)/3));
end

추가 답변 (1개)

Stephen23
Stephen23 2019년 2월 9일
>> D = datetime('01-Jan-2018');
>> N = 14;
>> G = 3;
>> V = D + caldays(0:ceil(N/G)-1).';
>> V = repelem(V,G);
>> V = V(1:N)
V =
01-Jan-2018
01-Jan-2018
01-Jan-2018
02-Jan-2018
02-Jan-2018
02-Jan-2018
03-Jan-2018
03-Jan-2018
03-Jan-2018
04-Jan-2018
04-Jan-2018
04-Jan-2018
05-Jan-2018
05-Jan-2018

카테고리

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