       Re: split again

• To: mathgroup at smc.vnet.net
• Subject: [mg73666] Re: split again
• From: "dkr" <dkrjeg at adelphia.net>
• Date: Sat, 24 Feb 2007 02:15:32 -0500 (EST)
• References: <ermd8n\$hmd\$1@smc.vnet.net>

```On Feb 23, 4:48 am, Arkadiusz.Ma... at gmail.com wrote:
> Hi,
>
> Thank you very much for your help in my provious post. Now, consider
>
> z=Table[Random[],{1000}];
>
> zSplit=Split[z,#1<=0.7 === #2>=0.7];           (bulit thanks to your
> help)
>
> I want to pick the first sublist of zSplit that consists of elements
> <= 0.7 and whose length is greater than a certain number (say 5). I
> think that a good candidate would be using Cases and variations of _ ,
> but I don't know how.
>
> What I want to do (and what my both posts are about) is to find the
> first sublist of z with all elements less than A and the length of
> this sublist must be greater than B. Maybe there exists better
> solution than to Split z in advance since what I need to do in my next
> step is to find ONLY the FIRST sublist of splitted z.
>
> Thanks again,
>
> Arek

Arek,

Your requirement that you want to find the first sublist of length
greater than B, with all elements less than the threshold A, is
ambiguous. Consider the trivial list {7,7,7,7,7}, with A=8 and B=2.
Then the sublists consisting of the first three elements, the first
four elements, and the whole list itself all meet your length and
threshold requirements, and all begin with the first element of the
list.  So which one do you want?  Below, fnShort will generate the
shortest such sublist, and fnLong and fnSplit will generate the
longest such sublist.

In:=
fnShort[x_List,thres_,len_]:=x/.{a___,(b__?(#<thres&))/;Length[{
b}]>len,c___}:>{b};
fnLong[x_List,thres_,len_]:=
x/.{a___,(b__?
(#<thres&))/;Length[{b}]>len,c___/;Length[{c}]==0||\
First[{c}]>=thres}:>{b};
fnSplit[x_List,thres_,
len_]:=Cases[Split[x,#1<thres&&#2<thres&],b_/;Length[
b]>len,{1},1]//Flatten;

In:=
test1={3,3,3,3,3};
test2={6,5,1,2,2,3.1,3,3,3,3,3,6,2,2,2,2};
A=3.5; B=3;

In:=
{fnShort[test1,A,B],fnLong[test1,A,B],fnSplit[test1,A,B]}

Out=
{{3,3,3,3},{3,3,3,3,3},{3,3,3,3,3}}

In:=
{fnShort[test2,A,B],fnLong[test2,A,B],fnSplit[test2,A,B]}

Out=
{{1,2,2,3.1},{1,2,2,3.1,3,3,3,3,3},{1,2,2,3.1,3,3,3,3,3}}

dkr

```

• Prev by Date: Re: Fw: 2
• Next by Date: Re: split again
• Previous by thread: Re: split again
• Next by thread: Re: split again