in reply to Hash-type lookup in PDL

Could you possibly work up a simple running script, with sample data, so we can see what format your data is in, and the dimensions of your piddles? It sounds to me my you are looking for "slices", to do the changes in place (efficiently), but I have no idea what your piddle looks like.

The cool thing about slices, is that whatever operation you perform on the slice, also occurs to the original piddle.

#!/usr/bin/perl use warnings; use strict; use PDL::LiteF; my $x = sequence(25); # Create a piddle of increasing value print "$x\n"; my $slice = slice $x,'4:20:2'; print $slice; print "\n"; $slice .= 0; print $slice; print "\n"; print $x; print "\n"; for (1..100){ $slice ++; print $x; print "\n"; select(undef,undef,undef,.5); }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: Hash-type lookup in PDL
by andye (Curate) on Jan 05, 2007 at 18:18 UTC
    Hi zentara, It's nothing complicated I'm looking to do, really, just lookup each value in a hash. Here's an example that doesn't work:
    #!/usr/bin/perl use warnings; use strict; use PDL; my $x = sequence(5); print "$x \n"; my $lookup = { 0 => 101, 1 => 69, 2 => 42, 3 => 10042, 4 => 99 }; my $y = zeroes(5); $y .= $lookup->{$x}; print "$y \n";
    That gives this output:
    [0 1 2 3 4] [0 0 0 0 0]
    I would be a happy bunny if it gave this output:
    [0 1 2 3 4] [101 69 42 10042 99]
    Best wishes, andye

    (I tried using a straightforward my $y = $lookup->{$x} but that doesn't produce any value at all)

    update: I've tried creating a pdl to use insread of the hash, like this:

    my $pdl_table = pdl([keys %$lookup], [values %$lookup]); my $y = zeroes(5); $y .= $pdl_table->(which($pdl_table->(:,0) == $x),1 +); print "$y \n";
    but that has two problems:
    • It's rather hard to read
    • It doesn't work
    Looks like I've screwed up the syntax in the which() statement, and I can't work out how to make it right - a dummy dimension somewhere?

      Hi andye,

      If your lookup table is rather small, you could try this code:

      #!/usr/bin/perl use warnings; use strict; use PDL; my $x = sequence(5); print "$x \n"; my @pdl_keys = (0, 1, 2, 3, 4); my @pdl_values = (101, 69, 42, 10042, 99); my $y = zeroes(5); foreach my $i (0..@pdl_keys-1){ my $indx = which($x == $pdl_keys[$i]); $y->dice($indx) .= $pdl_values[$i]; } print "$y \n";

      I hope this helps

      Cheers,

      lin0
        Many thanks zentara and lin0,

        Well, I went ahead and did it with my original approach of: convert the pdl to a perl list, then use a perl hash to look it up, then convert it back again. The batch job finished about half a day later, so it's an academic issue now really.

        Still, (as a note for for posterity) there seem really to be two options being discussed here:

        Either:

        - a PDL lookup table, using which() or dice() to look up one value at a time (slow on big datasets),

        or

        - convert all the values in advance, as per zentara's code above...

        Neither's ideal really... could be that converting to a perl scalar and using a hash is actually the best thing.

        Thanks for your help, and best wishes,
        andye