Hello
astroman,
I took some ideas from your reply to djerius to create this solution. I think this will work for you, and it keeps all the operations in piddles/PDL. Basically, I create masks for your greater than and less than or equal to conditions and apply them to "expanded" versions of your $a and $b piddles.
#!/usr/bin/env perl
use strict;
use warnings;
use PDL;
# The number of intervals you require (your example shows 3)
my $n = 3;
my $a = pdl (0,6,18,7,19,3,10,2,12,4,8,9,1,15,11,11,19,17,0,9);
my $b = pdl (0,1,2,3,4,5,6,7,8,9,10,10.5,11,11.5,12,12.5,13,13.5,14,14
+.5);
my $gt = sequence($n)*5; # equivalent to pdl(0, 5, 10) for $n = 3
my $ltoe = (sequence($n)+1)*5; # equivalent to pdl(5, 10, 15) for $n =
+ 3
my $expanded_a = $a->dummy(0,$n);
my $expanded_b = $b->dummy(0,$n);
my $gt_mask = $expanded_b > $gt;
my $ltoe_mask = $expanded_b <= $ltoe;
my $mask = $gt_mask & $ltoe_mask;
my $mask_w_bad = $mask->setbadif($mask == 0);
my $masked = $expanded_a * $mask_w_bad;
my $medians = $masked->transpose->medover;
print $medians, "\n";
exit;