Here is one way to achieve this without constructing a loop that iterates over each element of the pdl. I'm not sure that this would be significantly faster than just iterating over the elements of the pdl...you would need to benchmark it (also you may want to consider comparing it to a plain-perl solution).
#!/usr/bin/env perl use strict; use warnings; use PDL; use PDL::NiceSlice; my $dec = pdl [[1,1,2,2,3],[10,15,13,11,6]]; my $groups_vec = $dec(:,0); my $groups = $groups_vec->uniq; my $groups_t = $groups->transpose; my $values = $dec(:,1); my $masks = ($groups_t == $groups_vec); my $group_values = $masks*$values; my $max_of_each_group = $group_values->maximum; my $final_pdl = pdl($groups, $max_of_each_group)->transpose; print "Original Data: $dec\n"; print "Groups: $groups\n"; print "Transposed Groups: $groups_t\n"; print "Masks: $masks\n"; print "Group values: $group_values\n"; print "Max of each group: $max_of_each_group\n"; print "Final pdl: $final_pdl\n"; exit;
Gives the following output:
Original Data: [ [ 1 1 2 2 3] [10 15 13 11 6] ] Groups: [1 2 3] Transposed Groups: [ [1] [2] [3] ] Masks: [ [1 1 0 0 0] [0 0 1 1 0] [0 0 0 0 1] ] Group values: [ [10 15 0 0 0] [ 0 0 13 11 0] [ 0 0 0 0 6] ] Max of each group: [15 13 6] Final pdl: [ [ 1 15] [ 2 13] [ 3 6] ]

In reply to Re^3: PDL threading where/which instead of loop by kevbot
in thread PDL threading where/which instead of loop by roark

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.