2
$\begingroup$

i have following bins and binranges:

ages = {3, 12, 24, 15, 5, 74, 23, 54, 31, 23, 64, 75};
binranges = {0, 10, 25, 50, 75};
PositionIndex[BinLists[ages, {binranges}] 

will give me indices of bins within binranges. how can i relate these indices to ages so it would be [ 1 2 2 2 1 4 2 4 3 2 4] ?

$\endgroup$
1
$\begingroup$
bl = BinLists[ages, {binranges}];
Flatten@Most[Pick[Range@Length@bl, 
     Function[{x}, Intersection[x, {#}] === {#}] /@ bl] & /@ ages]

{1, 2, 2, 2, 1, 4, 2, 4, 3, 2, 4}

Also

Flatten[Position[bl, {___,#,___}]&/@ages]

{1, 2, 2, 2, 1, 4, 2, 4, 3, 2, 4}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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