Diviser un tableau avec des conditions de plage
J'ai un tableau comme suit:
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
La première colonne de a été triée. Je voudrais diviser ce tableau avec des conditions que la première colonne dans la plage ex. 0-2, 2-4, 4-6... Alors sortedransam
ça devrait être comme
{{{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}}}
Dois-je utiliser SplitBy
ou autre chose ? Je ne sais pas comment faire appliquer les conditions.
Réponses
5 kglr
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}}}
Vous pouvez également utiliser BinLists:
Join @@ BinLists[sortedransam, {0, 10, 2}, {-1, 1, 2}]
même résultat
4 J.M.'sennui
Pas trop dur avec 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}}}