The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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 ]

Posts: 9,850
Registered: 6/7/07
Re: vectorization addtodate
Posted: Mar 30, 2013 10:11 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 3/28/2013 11:08 PM, pmassicotte wrote:
> On Thursday, March 28, 2013 12:01:04 PM UTC-4, pmassicotte wrote:

>> addtodate(datenum('1-jan-2000'), 1:10, 'day'); %add 1 day

> Thank you for these clear explainations.

Well, it struck me that perhaps this was a place to play w/ my new toy
that has arrayfun() and all those other new things--

So I first tried,

>> d0=datenum('1-jan-2000');
>> arrayfun(@(a,b,c) (addtodate(a,b,c)), d0, [1:4]', 'day')

OK, it doesn't like the character string for the time field selector and
unfortunately, addtodate() won't accept the field as an index.

It is possible to work around it if one looks inside the addtodate()
m-file and sorta' cheats. addtodate calls a private mex function
addtodatemx() that does the actual work and uses the lookup table
position for the time field instead of the string so that gets around
the above problem.

Just for grins I made a private subdir in my working directory and made
a local copy of the addtodatemx and just to prove the point created

function [dn] = addtodatevec(date0,delta,field)
%ADDDATE - version vectorized...

fields = {'year','month','day', ...
'hour', 'minute', 'second', 'millisecond'};
field = find(strncmpi(fields,field,length(field)), 1);

dn = arrayfun(@(a,b,c) ...
(addtodatemx(a,b,c)), d0, delta, fld);

This allows the following--

>> myadddate(d0,[1:4]','day')
ans =

as requested. To do it requires either making a copy of the private
function addtodatemex in a private subdir below where you place the
above or adding the vectorized function to the existing subdir (not

Mostly an experiment since my previous release of ML didn't have
arrayfun() I was interested to try to make it work...

Would be interested if there's a way to make arrayfun work w/ a
combination of scalars and arrays w/o the explicit repmat() -- seems
like a logical extension that could be handled internally.


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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.