in reply to Grouping one piddle based on ranges of another

Update 1:

just noted you want a threaded way of doing this; this isn't it

Update 2:

There's a fundamental problem with threading this. If your $b piddle is typical, your data subsets won't have the same number of matching elements in them, which makes it impossible to thread over.

# calculate the number of elements in each chunk pdl> $idx = ($b/5)->floor pdl> $idx -= ($b - $idx * 5) == 0 pdl> p [$qidx->hist(-1,4,1)]->[1] [1 5 5 9 0]

Original Response:

Hi,

Note:Your updated code doesn't compile and when fixed doesn't give the stated results, e.g.:

pdl> p pdl($a(($b>0)*($b<=5);?)->medover, $a(($b>5)*($b<=10);?)->medov +er, $a(($b>10)*($b<=15);?)->medover) [7 8 11]
How about this:
pdl> $start = 0; pdl> $step = 5; pdl> $d = pdl( map { $a(($b > $start + $step *$_ )*( $b <= $start + $ +step*($_+1) );?)->medover } 0..3 ) pdl> p $d [7 8 11 13]

Replies are listed 'Best First'.
Re^2: Grouping one piddle based on ranges of another
by astroman (Novice) on Aug 27, 2015 at 01:23 UTC

    My apologies; I had modified the appearance to try to make it more clear what I was attempting. I've fixed it now and the code runs as written.

    Yes, you are correct about the intermediate piddles (the ones of which I am finding the median) being different lengths. I was thinking perhaps there's some way to expand into extra dimensions, where each dimension would contain the values in a given $b range, and then take medover the entire array to condense it into the form of $d. Is this possible?

    Like this, only without having to manually type all of the $mask(..) .= 1 lines:

    pdl> $e = $a(,,*3)->copy pdl> p $e [ [ [ 0 6 18 7 19 3 10 2 12 4 8 9 1 15 11 11 19 17 0 9] ] [ [ 0 6 18 7 19 3 10 2 12 4 8 9 1 15 11 11 19 17 0 9] ] [ [ 0 6 18 7 19 3 10 2 12 4 8 9 1 15 11 11 19 17 0 9] ] ] pdl> $mask = $e->zeroes pdl> $mask(,,0)->where(($b>0)*($b<=5)) .= 1 pdl> $mask(,,1)->where(($b>5)*($b<=10)) .= 1 pdl> $mask(,,2)->where(($b>10)*($b<=15)) .= 1 pdl> p $mask [ [ [0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0] ] [ [0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0] ] [ [0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1] ] ] pdl> $mask = $mask->setbadif($mask==0) pdl> p $mask [ [ [BAD 1 1 1 1 1 BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD + BAD BAD BAD] ] [ [BAD BAD BAD BAD BAD BAD 1 1 1 1 1 BAD BAD BAD BAD BAD BAD + BAD BAD BAD] ] [ [BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD 1 1 1 1 1 1 + 1 1 1] ] ] pdl> $f = $e*$mask pdl> p $f [ [ [BAD 6 18 7 19 3 BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD + BAD BAD BAD] ] [ [BAD BAD BAD BAD BAD BAD 10 2 12 4 8 BAD BAD BAD BAD BAD BAD + BAD BAD BAD] ] [ [BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD BAD 9 1 15 11 11 19 + 17 0 9] ] ] pdl> $g = $f->medover pdl> p $g [ [ 7] [ 8] [11] ]

    Thank you very much for your help!