Path: news.mathworks.com!newsfeed-00.mathworks.com!NNTP.WPI.EDU!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Newsgroups: comp.soft-sys.matlab
Subject: Re: Memory error on subsequent assignment
Date: Tue, 8 Jul 2008 04:15:44 +0000 (UTC)
Organization: National Research Council Canada - Conseil national de rechereches Canada
Lines: 31
Message-ID: <g4uplg$qia$1@canopus.cc.umanitoba.ca>
References: <g4uoq1$20v$1@fred.mathworks.com>
NNTP-Posting-Host: origin.ibd.nrc.ca
X-Trace: canopus.cc.umanitoba.ca 1215490544 27210 192.70.172.160 (8 Jul 2008 04:15:44 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Tue, 8 Jul 2008 04:15:44 +0000 (UTC)
Originator: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Xref: news.mathworks.com comp.soft-sys.matlab:478077



In article <g4uoq1$20v$1@fred.mathworks.com>,
Mark  <nospamthank@yahoo.com> wrote:
>I know most of the memory error issues (contiguous blocks
>etc) but can't quite understand why it is I'm getting an out
>of memory error on a particular line. I do the right thing
>and pre-allocate the memory block using...
>
>mth_ret = zeros(nsims, mths, ac, 'single');
>
>but get the out of memory error in the subsequent loop that
>performs variable assignment...
>
>for j = 1:ac
>    mth_ret(:,:,j) = <variable>.<field>.<var>(1:nsims,:);
>end

When you take a sub-section of a vector and it isn't immediately
obvious to Matlab that you are copying vectors of the same size
around, then Matlab may have to take a temporary copy of the source data
as part of the assignment process. The taking of that temporary
copy may fill up memory.

I -believe- Matlab is smart enough to get away without temporary
copies in some straight-forward cases, but my -expectation- is that
those cases would be only when entire columns are copied. I speculate
that it is possible that if you were to reverse the first and second
dimensions of the source and destination that Matlab might perhaps
be able to get away without creating a temporary copy.
-- 
  "I like to build things, I like to do things. I am having
  a lot of fun."                              -- Walter Chrysler