Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news1.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe01.iad.POSTED!7564ea0f!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
Organization: Canada Eat The Cookie Foundation
User-Agent: Thunderbird 2.0.0.17 (Windows/20080914)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: Memory problems
References: <12408444.1226087385326.JavaMail.jakarta@nitrogen.mathforum.org>
In-Reply-To: <12408444.1226087385326.JavaMail.jakarta@nitrogen.mathforum.org>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 86
Message-ID: <i3kRk.169$M_1.58@newsfe01.iad>
NNTP-Posting-Host: 24.79.146.116
X-Complaints-To: internet.abuse@sjrb.ca
X-Trace: newsfe01.iad 1226165582 24.79.146.116 (Sat, 08 Nov 2008 17:33:02 UTC)
NNTP-Posting-Date: Sat, 08 Nov 2008 17:33:02 UTC
Date: Sat, 08 Nov 2008 11:33:07 -0600
Xref: news.mathworks.com comp.soft-sys.matlab:499662


juckou wrote:
>Then I've used:
> 
> mymatrix=zeros(365,525,506);%506 images of 365 rows and 525 columns each 
> 
> to force matlab to use just the right amount of memory. Then the code runs fine but when I try
> to write mymatrix into a file it shows again the "out of memory" error.

Your code to write out the data makes a complete copy of the data, so it is not surprising
that you run out of memory.

> function flag = gwrite(name,tab)
> 
> [imax,jmax,kmax] = size(tab);
> valex = 9999;
> nbmots = imax*jmax*kmax;
> 
> c4 = reshape(tab,imax*jmax*kmax,1);

Another way of writing the above is:

valex = 9999;
c4 = tab(:);

But I would leave c4 in the original shape instead of making a vector
out of it.

> c4(find(isnan(c4))) = valex;

Use logical indexing instead of find:

c4(isnan(c4)) = valex;

Or, more efficient:

valex = 9999;
c4 = tab;
c4(isnan(c4)) = valex;


> [flag]=uwrite(name,c4,imax,jmax,kmax,valex,nbmots);

Just call

flag = uwrite(name, c4);

As you would be passing in the full shape of c4, the rest of the
parameters can be deduced, except valex (and see the below about that.)

 
> function [flag]=uwrite(file,c4,imax,jmax,kmax,valex,nbmots)
> flag=0;
> file;
> dummy=0;
> dummyf=0.;
> dummy24=24;
> iprec=4;
> fid=fopen(file,'w','ieee-be');
> if fid~=-1
> ind=find(isnan(c4));
> c4(ind)=valex;

There can't be any nan in c4 at this point: you eliminated them all
before you passed c4 in to this routine. The only effect of this is part
of the code is to make yet another copy of the data.

> if nbmots==-1
>  nbmots=imax*jmax*kmax
> end

[imax, jmax, kmax] = size(c4);
nbmots = numel(c4);

I suspect several parts of the rest of the code can be cleaned up, but I
have not checked them out in detail.

> I don't think the variable is that big to run out with all the memory.

You need enough -contiguous- memory. And you have to stop making unnecessary
copies of your variables.

-- 
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?