Re: Stop iteration iconstructing Table
- To: mathgroup at smc.vnet.net
- Subject: [mg90603] Re: Stop iteration iconstructing Table
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Tue, 15 Jul 2008 06:18:39 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g59tn8$omh$1@smc.vnet.net> <g5f5ug$rbu$1@smc.vnet.net>
dh wrote:
> Hi Angel,
>
> if you stop a Table command, it does not return the result. You may do
>
> this using a breakable loop (Do,While,For) and Append. However, if you
>
> use an ordinary list to append to, this is very inefficient, as the
>
> whole list is reallocated each time. Therefore, the it is much faster to
>
> create a nested list, that does not need reallocation. E.g. to create a
>
> list from 1..5 with a loop that runs to 10
>
> list={};
>
> Do[
>
> AppendTo[list,{i}];
>
> If[i>=5,Break[]];
>
> ,{i,10}];
>
> list= Flatten[list]
>
> hope this helps, Daniel
Just to add to Daniel's excellent advice, you could investigate how to
use the Sow[] and Reap[] functions. For instance,
In[1]:= Reap[Do[If[i >= 5, Break[], Sow[i]], {i, 10}]][[2, 1]]
Out[1]= {1, 2, 3, 4}
Regards,
-- Jean-Marc
> malawk at gmail.com wrote:
>
>> Hi
>
>
>> Sorry I'm not very familiar with Mathematica. I want to create a
>
>> simple Table of this sort:
>
>
>> Table[{{$1, $2, $3, ...}, {...}, ..., {...}}, {i,1,imax}],
>
>
>> where $2 decreases as the iteration proceeds, and imax should be such
>
>> that only those elements with $2 >= 100 are returned. When $2 goes
>
>> below 100 then the iteration should stop and the final Table given. I
>
>> want to do this because I'm not interested in computing those elements
>
>> whose $2 <100.
>
>
>> Any ideas how can I do this simple thing?
>
>
>> Thank you so much!
>
>> Angel
>
>
>
>
>
>
>
>