Re: Loop, deleting intermediary results, lists
- To: mathgroup at smc.vnet.net
- Subject: [mg78767] Re: Loop, deleting intermediary results, lists
- From: Peter Pein <petsie at dordos.net>
- Date: Mon, 9 Jul 2007 01:33:44 -0400 (EDT)
- References: <f6qed5$a43$1@smc.vnet.net>
kristoph schrieb:
> Dear all,
>
> Assume we have f[x_]:=f[x]=x^2 within some For[x=1,x<=3,x++...] loop.
> At each iteration we assign a value to f[x] which results in a total
> of three values.
>
> I would like to delete at each iteration x the previous result of f.
> Thus, we would only have a result of f[3] but not for the f[1] and
> f[2].
>
> This is a very simplified example but I think it describes my problem
> well enough. The problem I have is that I generate via a loop a
> massive amount of data in form of a list which I combine in one big
> list but I don't need the intermediary results. At the moment
> Mathematica 5.2 is always crashing and I think it has to do with these
> lists.
>
> I didn't us f[x-1]={} within the loop so far but I thought there might
> be a more elegant way to handle the problem.
>
> Thanks in advance,
> Kristoph
>
>
Hi Kristoph,
if you need only f[x-1 to calculate f[x], I do not understand, why you
do not write
In[30]:=
fxminus1=0;
For[x=1,x<=3,x++,
fx=fxminus1+2x-1;
Print[fx];
fxminus1=fx];
Information@fxminus1
which gives
1
4
9
Global`fxminus1
fxminus1 = 9
or even simpler
Fold[(Print[#]; #) &[#1 + 2#2 - 1] &, 0, Range[3]]
and no storage is in use for obsolete values.
But if you insist on doing it your way, try If[x>1,f[x-1]=.] at the end
of your loop body or, if you calculate some recursive f, define f[0] in
the start of your loop
For[f[0]=0;x=1,x<=3,x++,f[x_]:=f[x]=f[x-1]+2x-1;Print[f[x]];f[x-1]=.]
?f
1
4
9
Global`f
f[3] := 9
f[x_] := f[x] = x^2
No f[1|2] any more. :-)
Peter