Re: Consecutive runs in mathematica

From: Tony King (mathstutoring_at_ntlworld.com)
Date: 03/09/05


Date: Wed, 09 Mar 2005 14:38:50 GMT

Thank you - I have modified your code slightly and it seems to be working
perfectly

In[72]:=
v = Table[trialQ[k], {k, 1, 1000000}];
In[73]:=
t = Split[v];

({Max[(If[First[#1] === True, Length[#1], 0] & ) /@ t],

Max[(If[First[#1] === False, Length[#1], 0] & ) /@ t]}) // Timing

Out[74]=
{4.157 Second, {5, 2}}

"Dana" <delouis@bellsouth.net> wrote in message
news:haCXd.10644$c72.2059@bignews3.bellsouth.net...
> Don't know if this would be efficient for a large array (million terms),
> but here is one way. I understand your "true/false" to be variables, and
> not the built-in True & False.
>
> v = {true, true, false, true, true, true, false, true};
>
> t = Split[v]
> {{true, true}, {false}, {true, true, true}, {false}, {true}}
>
> Max[(If[First[#1] === true, Length[#1], 0] & ) /@ t]
> 3
>
> Max[(If[First[#1] === false, Length[#1], 0] & ) /@ t]
> 1
>
>
> --
> Dana DeLouis
> Win XP & Office 2003
>
>
> "Tony King" <mathstutoring@ntlworld.com> wrote in message
> news:hiBXd.138$194.116@newsfe2-gui.ntli.net...
>>I have a list which looks something like
>>
>> {true,true,false,true,true,true,false,true.........}
>>
>> that contains something like a million terms, and I would like some code
>> to return the maximum length of consecutive runs of trues and the maximum
>> length of consecutive runs of false.
>>
>> Any ideas / assistance would be greatly appreciated.
>>
>> Best wishes
>>
>> Tony
>>
>
>



Relevant Pages

  • Re: Who can explain this LOOKUP formula?
    ... In Excel, True is numerically equivalent to 1 and False is numerically ... when you divide that whole array of Trues and False ... This array is called the LookupVector. ...
    (microsoft.public.excel.misc)
  • Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?
    ... nothing to put in it then it should return an empty array, ... Likewise if any PHP function expects an array and you give it something else ... Tony Marston ... > "Tony Marston" wrote in message ...
    (comp.lang.php)
  • Re: Array and resize
    ... I think William is not having the problem of trying to place the 4x8 ... I myself am mystified by this as well, but I have some knowledge of Excel ... The only thing Dana DeLouis does is convert the original matrix (an array ...
    (microsoft.public.excel.programming)
  • Re: Array and resize
    ... The example you gave was an array of three 1x3 arrays, and Dana DeLouis showed you how to dump the corresponding 3x3 matrix into A1:C3. ... Sub DumpValues() ... can't drop these onto a worksheet in this way. ...
    (microsoft.public.excel.programming)
  • Re: Shorten Code
    ... Thats really sweet and works great. ... "Dana DeLouis" wrote: ... If you go crazy with too large an array, it can be hard to read and debug. ...
    (microsoft.public.excel.programming)