3
$\begingroup$

I have an array like following:

    sortedransam={{0.105328, -0.0291632}, {0.253571, 0.00498561}, {0.410887, 
  0.171317}, {1.45579, 0.300952}, {2.56002, -0.0599007}, {3.67651, 
  0.0913857}, {4.44773, -0.21599}, {4.68098, 0.0766649}, {5.20004, 
  0.0153934}, {5.31011, 0.157674}, {6.25626, -0.119345}, {6.35928, 
  0.145992}, {6.52711, -0.0163245}, {7.44436, 0.0334628}, {7.8401, 
  0.305493}, {8.18541, 0.0712892}, {8.21423, -0.0325363}, {9.0921, -0.0242404}, {9.3285, 0.035512}}

sortedransam's first colume has been sorted. I would like to split this array with conditions that the first column into range ex. 0-2, 2-4, 4-6... Then the sortedransam should be like

    {{{0.105328, -0.0291632}, {0.253571, 0.00498561}, {0.410887, 
  0.171317}, {1.45579, 0.300952}},
  {{2.56002, -0.0599007}, {3.67651, 0.0913857}},
  {{4.44773, -0.21599}, {4.68098, 0.0766649}, {5.20004, 0.0153934}, {5.31011, 0.157674}},
  {{6.25626, -0.119345}, {6.35928, 0.145992}, {6.52711, -0.0163245}, {7.44436, 0.0334628}, 
  {7.8401,0.305493}},
  {{8.18541, 0.0712892}, {8.21423, -0.0325363}, {9.0921, -0.0242404}, {9.3285, 0.035512}}}

Should I use SplitBy or something else? I don't know how to get conditions implemented.

$\endgroup$

2 Answers 2

5
$\begingroup$
SplitBy[sortedransam, Quotient[First[#], 2] &]
 {{{0.105328, -0.0291632}, {0.253571, 0.00498561}, {0.410887, 0.171317}, {1.45579, 0.300952}},
 {{2.56002, -0.0599007}, {3.67651, 0.0913857}}, 
 {{4.44773, -0.21599}, {4.68098, 0.0766649}, {5.20004, 0.0153934}, {5.31011, 0.157674}}, 
 {{6.25626, -0.119345}, {6.35928, 0.145992}, {6.52711, -0.0163245}, 
  {7.44436, 0.0334628}, {7.8401, 0.305493}},
 {{8.18541, 0.0712892}, {8.21423, -0.0325363}, {9.0921, -0.0242404}, {9.3285, 0.035512}}}

You can also use BinLists:

Join @@ BinLists[sortedransam, {0, 10, 2}, {-1, 1, 2}]

same result

$\endgroup$
4
$\begingroup$

Not too hard with GatherBy[] + Floor[]:

GatherBy[sortedransam, Floor[First[#], 2] &]
   {{{0.105328, -0.0291632}, {0.253571, 0.00498561}, {0.410887, 0.171317},
     {1.45579, 0.300952}},
    {{2.56002, -0.0599007}, {3.67651, 0.0913857}},
    {{4.44773, -0.21599}, {4.68098, 0.0766649}, {5.20004, 0.0153934},
     {5.31011, 0.157674}},
    {{6.25626, -0.119345}, {6.35928, 0.145992}, {6.52711, -0.0163245},
     {7.44436, 0.0334628}, {7.8401, 0.305493}},
    {{8.18541, 0.0712892}, {8.21423, -0.0325363}, {9.0921, -0.0242404},
     {9.3285, 0.035512}}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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