Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider the following list:

data={4078, 256, -1266, -388, 1290, 1368, 1446, 1524, 2};

I would like to split any kind of such list always, when figures turn form negative to positive. In the case of data the result would be:

{{4078, 256, -1266, -388},{ 1290, 1368, 1446, 1524, 2}}

I tried it with

Split[%, #2>0 && #1<0 &]

but I got:

{{4078}, {256}, {-1266}, {-388, 1290}, {1368}, {1446}, {1524}, {2}}
share|improve this question

3 Answers 3

up vote 12 down vote accepted

Try

Split[data, Not[#2>0 && #1<0] &]

Note that Split[list, test] splits list between two elements when test fails. In this case you want to to split the list iff #2>0 && #1<0 &[e1, e2] == True which is equivalent to Not[#2>0 && #1<0]&[e1, e2] == False.

share|improve this answer
    
Heike, you are indeed a great help! :) –  John Apr 12 '12 at 22:32

Alternately:

In[277]:= Split[data, Sign[{##}] != {-1, 1} &]

Out[277]= {{4078, 256, -1266, -388}, {1290, 1368, 1446, 1524, 2}}

In[276]:= Split[data, GreaterEqual @@ Sign[{##}] &]

Out[276]= {{4078, 256, -1266, -388}, {1290, 1368, 1446, 1524, 2}}
share|improve this answer

Probably the tersest way to write it

Split[data, ! #1 < 0 < #2 &]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.