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

In reply to Re: Working with PDL, need index range of consecutive values by plobsing
in thread Working with PDL, need index range of consecutive values by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.