Date: Mar 28, 2013 4:44 PM
Author: dpb
Subject: Re: vectorization addtodate

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.

--