Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

For my own questionquestion and self-answer I wrote the following utility function.

BinPositions[vals_, brakes_, sameTest_] :=
 Map[val \[Function] 
   Catch@Module[{lo = 1, mid, hi = Length[brakes], el, res},
     While[lo <= hi, Which[
       TrueQ@sameTest[val, el = brakes[[mid = Floor[(lo + hi)/2]]]], 
       Throw[mid],
       el > val, hi = mid - 1,
       True, lo = mid + 1]];
     lo - 1/2],
  vals]

The semanthic is sligtly different from previous answer, but, maybe, it can be adapted to your scenario.

list = RandomReal[{0, 100}, {20}]
brakes = Union@RandomInteger[{0, 100}, 10]
Floor@BinPositions[list, brakes, SameQ]

{48.311, 40.7217, 50.8321, 73.2729, 24.7769, 42.4868, 12.475, 7.04288, 3.22704, 89.9731, 82.0066, 73.565, 83.0642, 45.4985, 81.3878, 55.5902, 82.3677, 68.8531, 57.4473, 83.1311}

{6, 20, 28, 32, 35, 52, 73, 82, 95}

{5, 5, 5, 7, 2, 5, 1, 1, 0, 8, 8, 7, 8, 5, 7, 6, 8, 6, 6, 8}

For my own question and self-answer I wrote the following utility function.

BinPositions[vals_, brakes_, sameTest_] :=
 Map[val \[Function] 
   Catch@Module[{lo = 1, mid, hi = Length[brakes], el, res},
     While[lo <= hi, Which[
       TrueQ@sameTest[val, el = brakes[[mid = Floor[(lo + hi)/2]]]], 
       Throw[mid],
       el > val, hi = mid - 1,
       True, lo = mid + 1]];
     lo - 1/2],
  vals]

The semanthic is sligtly different from previous answer, but, maybe, it can be adapted to your scenario.

list = RandomReal[{0, 100}, {20}]
brakes = Union@RandomInteger[{0, 100}, 10]
Floor@BinPositions[list, brakes, SameQ]

{48.311, 40.7217, 50.8321, 73.2729, 24.7769, 42.4868, 12.475, 7.04288, 3.22704, 89.9731, 82.0066, 73.565, 83.0642, 45.4985, 81.3878, 55.5902, 82.3677, 68.8531, 57.4473, 83.1311}

{6, 20, 28, 32, 35, 52, 73, 82, 95}

{5, 5, 5, 7, 2, 5, 1, 1, 0, 8, 8, 7, 8, 5, 7, 6, 8, 6, 6, 8}

For my own question and self-answer I wrote the following utility function.

BinPositions[vals_, brakes_, sameTest_] :=
 Map[val \[Function] 
   Catch@Module[{lo = 1, mid, hi = Length[brakes], el, res},
     While[lo <= hi, Which[
       TrueQ@sameTest[val, el = brakes[[mid = Floor[(lo + hi)/2]]]], 
       Throw[mid],
       el > val, hi = mid - 1,
       True, lo = mid + 1]];
     lo - 1/2],
  vals]

The semanthic is sligtly different from previous answer, but, maybe, it can be adapted to your scenario.

list = RandomReal[{0, 100}, {20}]
brakes = Union@RandomInteger[{0, 100}, 10]
Floor@BinPositions[list, brakes, SameQ]

{48.311, 40.7217, 50.8321, 73.2729, 24.7769, 42.4868, 12.475, 7.04288, 3.22704, 89.9731, 82.0066, 73.565, 83.0642, 45.4985, 81.3878, 55.5902, 82.3677, 68.8531, 57.4473, 83.1311}

{6, 20, 28, 32, 35, 52, 73, 82, 95}

{5, 5, 5, 7, 2, 5, 1, 1, 0, 8, 8, 7, 8, 5, 7, 6, 8, 6, 6, 8}

Source Link
unlikely
  • 7.2k
  • 22
  • 54

For my own question and self-answer I wrote the following utility function.

BinPositions[vals_, brakes_, sameTest_] :=
 Map[val \[Function] 
   Catch@Module[{lo = 1, mid, hi = Length[brakes], el, res},
     While[lo <= hi, Which[
       TrueQ@sameTest[val, el = brakes[[mid = Floor[(lo + hi)/2]]]], 
       Throw[mid],
       el > val, hi = mid - 1,
       True, lo = mid + 1]];
     lo - 1/2],
  vals]

The semanthic is sligtly different from previous answer, but, maybe, it can be adapted to your scenario.

list = RandomReal[{0, 100}, {20}]
brakes = Union@RandomInteger[{0, 100}, 10]
Floor@BinPositions[list, brakes, SameQ]

{48.311, 40.7217, 50.8321, 73.2729, 24.7769, 42.4868, 12.475, 7.04288, 3.22704, 89.9731, 82.0066, 73.565, 83.0642, 45.4985, 81.3878, 55.5902, 82.3677, 68.8531, 57.4473, 83.1311}

{6, 20, 28, 32, 35, 52, 73, 82, 95}

{5, 5, 5, 7, 2, 5, 1, 1, 0, 8, 8, 7, 8, 5, 7, 6, 8, 6, 6, 8}