Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am working with PDL's for a project and have a question involving ranges of consecutive values, and generating a piddle based on the indexes there.

I have a PDL with 3 rows which correlate with each other. The first row is my values, the second is the time at which those values took place, and the third is a mask of 0's and 1's. For the purposes of what I'm doing, we can ignore the GMT row.

Here's a visual of what I'm working with:
$value = [1, 20, 60, 3, 900, 34, 93, 5, 12, 24, 16, 200] $mask = [ 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1 ]
As you can see, each pdl has 12 elements. What I need to do is take and create a new PDL of the elements in $value that have consecutive 0's in $mask. I also need to preserve their indexes within $values. As an example using the above, the output I desire is a piddle like this:
$a1 = [ [60,30,900] #from $values [2,3,4] #their original indexes ] and $a2 = [ [5,12,24] [7,8,9] ]
(Where the above is a pdl with 2 pdls inside)

These would be generated from the consecutive 0's which occur in the mask.

I hope that my wording is clear and illustrates my point properly. Any and all help on this would be appreciated, as there may be some PDL function that I am unaware of that would aid me here.

Thanks

Replies are listed 'Best First'.
Re: Working with PDL, need index range of consecutive values
by plobsing (Friar) on Feb 21, 2009 at 04:45 UTC

    Here's some building blocks (I'm too lazy for a full answer):

    # I assume you are using 2 different pdls, not rows of the same pdl as + you mentioned in your post # If it's just one pdl, 'dog' it my $value = pdl [ 1, 20, 60, 3, 900, 34, 93, 5, 12, 24, 16, 200 ]; my $mask = pdl [ 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1 ]; # get the indices of $mask that are 0 my $indices = which $mask == 0; # detect lower and upper edges of sequential regions of indices my $lower_edges = $indices - 1 != $indices->rotate(1); my $upper_edges = $lower_edges->rotate(-1); # also: # my $upper_edges = $indices + 1 != $indices->rotate(-1) # now slice and dice up your piddles knowing the regions you want.

    Alternative: detect edges of regions of equal value in the mask and then only select even (or odd, depending on the first value) numbered regions. I chose to filter first to eliminate data. You would avoid indirection through the $indices pdl latter by detecting region edges in the mask in stead of detecting region edges in the indices of the mask that are 0. Try both, keep the better (for some value of better) one.

    I feel there should be a more elegant way to detect edges of regions.

    Update: There is a more elegant way:

    use PDL::ImageND; my $edge_detector_kern = pdl [ -1, 1 ]; my $upper_edges = convolveND( $mask, $edge_detector_kern ); # where it is -1 is the upper edge of a region of zeroes # where it is 1 is the upper edge of a region of ones
      Thank you for your help in the matter. I am going to implement this soon and will keep you posted on the results! Thanks again.
      Update: I have tried your more elegant solution and this works perfectly for what I needed! Thank you so much for your help, and your explanation of things. I am very new to PDL (I'm a graduate student in Texas), and my bosses are familiar with PDL but not quite as savvy as you folk. Piddles take some getting used to, but I think I'm starting to get the hang of them.