Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: growing array in Matlab? (new code)
Date: Fri, 19 Aug 2005 07:40:11 -0400
Organization: The MathWorks
Lines: 76
Message-ID: <MPG.1d6f8f21579944ce98992f@news.mathworks.com>
References: <woodchips-EDAAE9.21275017082005@syrcnyrdrs-01-ge0.nyroc.rr.com> <MPG.1d6e3a12f3c423c98992c@news.mathworks.com> <woodchips-E55FA6.08284918082005@syrcnyrdrs-02-ge0.nyroc.rr.com> <woodchips-7262EC.11380018082005@syrcnyrdrs-03-ge0.nyroc.rr.com> <de2hup$qgu$1@ruby.cit.cornell.edu> <ef11420.19@webx.raydaftYaTP>
NNTP-Posting-Host: shurel.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
X-Trace: fred.mathworks.com 1124451611 29335 144.212.111.111 (19 Aug 2005 11:40:11 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 19 Aug 2005 11:40:11 +0000 (UTC)
User-Agent: MicroPlanet-Gravity/2.70.2067
Xref: news.mathworks.com comp.soft-sys.matlab:296891


In article <ef11420.19@webx.raydaftYaTP>, JohnCreighton_@hotmail.com 
says...
> Stephen Vavasis wrote:
> >
> >
> > Dear Loren and John,
> >
> > This is a feature of Matlab that is new to me and is very neat! It
> > seems to
> > me that this technique (storing data in a function handle of a
> > nested
> > function) may provide a workaround better than global variables to
> > bypass
> > the "call by value" limitation of Matlab. Perhaps this trick will
> > make it
> > possible to write objects that are efficient implementations of
> > data
> > structures like heaps, in which some of the methods are supposed to
> > update
> > the underlying data structure.
> >
> > -- Steve
> >
> >
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message
> >
> news:woodchips-7262EC.11380018082005@syrcnyrdrs-03-ge0.nyroc.rr.com.
> > ..
> >> In article
> >>
> <woodchips-E55FA6.08284918082005@syrcnyrdrs-02-ge0.nyroc.rr.com>
> ,
> >> John D'Errico <woodchips@rochester.rr.com> wrote:
> >>
> >>> In article
> <MPG.1d6e3a12f3c423c98992c@news.mathworks.com>,
> >>> Loren Shure <loren.shure@mathworks.com> wrote:
> >>>
> >>> > I have not tried anything here and don't know the
> performance
> >>> > implications, but instead of persistent, what if you
> made your
> > grow
> >>> > array function return a handle to a nested function
> which
> > stored the
> >>> > data there? That way, you could have more than
> instance.
> > Just a
> >>> > thought...
> >>> >
> >> It is an interesting idea. The code is below. It also
> >> runs in linear time, as did my last attempt. But it is
> >> also sadly twice as slow as was that last code. The
> >> profile tool suggests that 47% of the time was spent
> >> in function overhead. This explains where the doubled
> >> time came from.
> >>
> >> John
> >>
> >
> >
> I haven't read everything in this thread by what I gather is the data
> is stored in a persistent variable and not a function handle. The
> function handle just provides a key to that data. I'll read more to
> see if I understand this right. I don't see anything wrong with the
> global variable approach I will provide more information about this
> in my next post.
> 

Not true with nested functions that data is persistent, at least via the 
persistent keyword in MATLAB and meaning only one instance.  The 
relevant workspace accompanies the handle to the nested function.  
That's why you can have multiple instances.

--Loren