in reply to Re: PDL threading where/which instead of loop
in thread PDL threading where/which instead of loop

Sorry for not being clear. The values of $dec(:,0) will not be always 0 or 1. In fact they can take any number of values(from 1 to some maximum number), my goal is to compute -for each unique value - the maximum of corresponding values in $dec(:,1).
Thus
$dec = pdl [[1,1,2,2,3],[10,15,13,11,6]];

I want the output to be:
[ [ 1 15] [ 2 13] [ 3 6] ]
Hope this helps, thanks in advance..

Replies are listed 'Best First'.
Re^3: PDL threading where/which instead of loop
by kevbot (Vicar) on Aug 20, 2013 at 04:51 UTC
    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] ]