Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: vectorization addtodate
Replies: 3   Last Post: Mar 30, 2013 10:11 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
dpb

Posts: 8,260
Registered: 6/7/07
Re: vectorization addtodate
Posted: Mar 28, 2013 4:44 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 3/28/2013 11:01 AM, pmassicotte wrote:
...
> Would it be possible to vectorize addtodate? I would like to add
> many different days to a given date. However, addtodate only accept
> 1numeric scalar.
>
> addtodate(datenum('1-jan-2000'), 1, 'day'); %add 1 day
>
> But this won't work:
>
> addtodate(datenum('1-jan-2000'), 1:10, 'day'); %add 1 day
>
>
> It would be pretty easy to do with for loop, but I would like to avoid it if possible.


Just use datenum() directly...

A) Include start date...

>> datenum(2000,1, [1:10]')
ans =
730486
730487
730488
730489
730490
730491
730492
730493
730494
730495
>>

Or exclude start date...since days are trivial. This isn't good for
other time intervals because of either floating point roundoff if time
or irregular days/month, etc., for other than days.

>> datenum(2000,1, 1)+[1:10]'
ans =
730487
730488
730489
730490
730491
730492
730493
730494
730495
730496
>>

Exclude start date cleanly -- increment the '1' in the place field.

>> datenum(2000,1, 1+[1:10]')
ans =
730487
730488
730489
730490
730491
730492
730493
730494
730495
730496
>>

NB: that datenum is clever-enough that units roll over properly
accounting for days/month, leapyear, etc., etc., etc., when incrementing.

--




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.