Pisahkan larik dengan kondisi rentang
Saya memiliki array seperti berikut:
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
Kolom pertama telah disortir. Saya ingin membagi array ini dengan ketentuan bahwa kolom pertama menjadi rentang ex. 0-2, 2-4, 4-6 ... Maka sortedransam
harus seperti
{{{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}}}
Haruskah saya menggunakan SplitBy
atau yang lainnya? Saya tidak tahu bagaimana menerapkan kondisi.
Jawaban
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}}}
Anda juga bisa menggunakan BinLists:
Join @@ BinLists[sortedransam, {0, 10, 2}, {-1, 1, 2}]
hasil yang sama
4 J.M.'sennui
Tidak terlalu sulit dengan 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}}}
Kiat Pemilik Anjing yang Bermanfaat: Mengapa Penting untuk Membiarkan Anjing Anda Mengendus di Jalan
Taylor Sheridan Baru Menambahkan 1 Bintang 'Yellowstone' Favoritnya ke Pemeran 'Lawmen: Bass Reeves'