# Gather all elements in a list of floats into groups defined by a list of ranges?

A similar question regarding integer numbers was asked and answered here. However, since all the solutions there rely on generating the full set of numbers in the process of grouping them, which is not feasible in the case of floating point numbers, I'm asking the below question.

Consider a list of measurements {x,y} of some sorts, where x is a positive, monotonously increasing floating point number, for example:

list = {{1.1,2},{2.3,4},{4.5,5},{6.2,7},{8.3,8},{9.5,8}};


we also have a list of boundaries in x-space between which we would like to group the readings in list

boundaries = {1,5,7,10};


This particular list of boundaries tells us that we would like to group measurements in list for x-values between 1 and 5, between 5 and 7, and between 7 and 10. I would like to have a function group that does this:

group[list,boundaries]


{ {{1.1,2},{2.3,4},{4.5,5}} , {{6.2,7}} , {{8.3,8},{9.5,8}} }

Does Mathematica have such a function group, or maybe one can implement it efficiently? Of course, I could hack together a super ugly loop that would iterate through each element and append elements appropriately -- however, I'm wondering if there is a better, more efficient solution one could achieve through smarter Mathematica functions?

grouP = Join @@ BinLists[#, {#2}, {{-∞, ∞}}] &;

grouP[list, boundaries]

 {{{1.1, 2}, {2.3, 4}, {4.5, 5}}, {{6.2, 7}}, {{8.3, 8}, {9.5, 8}}}

grouP2 = GatherBy[SortBy[First]@#, Function[x, Total[UnitStep[x[] - #2]]]] &;

grouP2[list, boundaries]

 {{{1.1, 2}, {2.3, 4}, {4.5, 5}}, {{6.2, 7}}, {{8.3, 8}, {9.5, 8}}}

grouP3 = SplitBy[SortBy[First]@#, Function[x, Total[UnitStep[x[] - #2]]]] &;

grouP3[list, boundaries]

 {{{1.1, 2}, {2.3, 4}, {4.5, 5}}, {{6.2, 7}}, {{8.3, 8}, {9.5, 8}}}

grouP4 = Values @ GroupBy[SortBy[First]@#,
Function[x, Total[UnitStep[x[] - #2]]]] &;

grouP4[list, boundaries]

 {{{1.1, 2}, {2.3, 4}, {4.5, 5}}, {{6.2, 7}}, {{8.3, 8}, {9.5, 8}}}

• Wow, especially amazed by the nice Join@@ instead of a probably way less efficient Flatten[#, 1] & /@! Aug 23, 2020 at 19:28
Cases[list, {a_, b_} /; a > #1 && b <= #2] & @@@ Partition[boundaries, 2, 1]


{{{1.1, 2}, {2.3, 4}, {4.5, 5}}, {{6.2, 7}}, {{8.3, 8}, {9.5, 8}}}
{"ft_lang_label":"__label__en","ft_lang_prob":0.9458212,"math_prob":0.9793493,"size":1245,"snap":"2023-40-2023-50","text_gpt3_token_len":329,"char_repetition_ratio":0.09508461,"word_repetition_ratio":0.010362694,"special_character_ratio":0.28273094,"punctuation_ratio":0.20689656,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9843262,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-01T01:22:23Z\",\"WARC-Record-ID\":\"<urn:uuid:bdbf5914-cc55-424c-b3e5-43f06d69aa22>\",\"Content-Length\":\"172591\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:852b65a5-1600-4177-ab77-c210e7167b49>\",\"WARC-Concurrent-To\":\"<urn:uuid:1beb24d4-bf83-40d2-9910-aaac8e1168a6>\",\"WARC-IP-Address\":\"104.18.43.226\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/228936/gather-all-elements-in-a-list-of-floats-into-groups-defined-by-a-list-of-ranges\",\"WARC-Payload-Digest\":\"sha1:HDGJPMHDJYGSVAHW4N3S7BW55G2W6B3U\",\"WARC-Block-Digest\":\"sha1:5F6KFTLS6RUXT64GMC4SQPZPLLXZWAZ2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100258.29_warc_CC-MAIN-20231130225634-20231201015634-00378.warc.gz\"}"}