perlpdl

Does Perl PDL have a way to clamp a vector by min/max values given in another vector without iterating?


Does PDL already have a way to "clamp" vector elements to min/max values given in another vector? I've looked through the PDL::Math documentation but didn't see a good candidate option.

I could unpdl/re-pdl but that seems like too much overhead.

Is there already a vectorized way to do this?

I tried something like this:

$pdl = pdl [4.45, 5.55, 45];
$max = pdl [2   , 3,    50];
print ( (($max <=> $pdl) > 0)*$pdl + (($max <=> $pdl) < 0)*$max ) 
[2 3 45]

which seems to work, but what a crazy expression. Is there a built-in for this?


Solution

  • PDL::Primitive::hclip is for this:

    pdl> $pdl = pdl [4.45, 5.55, 45];
    pdl> $max = pdl [2   , 3,    50];
    pdl> p $pdl->hclip($max)
    [2 3 45]