MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: split

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73639] Re: split
  • From: Bill Rowe <readnewsciv at sbcglobal.net>
  • Date: Fri, 23 Feb 2007 04:45:11 -0500 (EST)

On 2/22/07 at 4:30 AM, Arkadiusz.Majka at gmail.com wrote:

>I want to split a list, say

>z = {1, 3, 2, 6, 4, 7, 5,1,7};

>into sublist of elements that are less or equal 3.

>so I want to obtain

>{{1,3,2},{6,4,7,5},{1},{7}}

>How to do it? Probably by applying Split, but what to put in Test?

>Split[z,#<=3&] gives :

>{{1, 3, 2, 6}, {4}, {7}, {5}, {1, 7}}

>Why 6 was put in first sublist together with 1, 3, and 2 since 6>3
>and should be together with 4 in the second sublist?

Because the test #<=3& in Split isn't grouping elements
together. Instead it says to end a sublist whenever you find an
element greater than 3. For this particular example, you can get
the desired result with

In[2]:=
Split[z, Abs[#2 - #1] <= 3 & ]

Out[2]=
{{1,3,2},{6,4,7,5},{1},{7}}

But this test compares the difference between adjacent elements
to 3 rather than the value of the element itself. And it will
fail to do what you want if the arrangement of the elements is
different. For example,

In[3]:=
Split[Range[6], Abs[#2 - #1] <= 3 & ]

Out[3]=
{{1, 2, 3, 4, 5, 6}}

clearly doesn't break up the list as desired.

One way to use Split to get the result you want would be to
create an indicator variable and remove it later, i.e.,

In[5]:=
(#1[[All,1]] & ) /@ Split[Transpose[{z, Sign[z - 3.5]}],
    Last[#1] == Last[#2] & ]

Out[5]=
{{1, 3, 2}, {6, 4, 7, 5}, {1}, {7}}

And this technique works for other arrangements of the elements
such as

In[6]:=
(#1[[All,1]] & ) /@
   Split[Transpose[{Range[6], Sign[Range[6] - 3.5]}],
    Last[#1] == Last[#2] & ]

Out[6]=
{{1, 2, 3}, {4, 5, 6}}
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: NonLinearRegression Weights
  • Next by Date: Re: NonLinearRegression Weights
  • Previous by thread: Re: split
  • Next by thread: Re: split