perlgreppdl

Perl PDL : Search if a vector is in an array or in a matrix


I try to make a grep like on a PDL matrix or array of Vector :

my @toto;
push(@toto, pdl(1,2,3));
push(@toto, pdl(4,5,6));
my $titi=pdl(1,2,3);
print("OK") if (grep { $_ eq $titi} @toto);

I also tried

my @toto;
push(@toto, pdl(1,2,3));
push(@toto, pdl(4,5,6));
my $titi=pdl(1,2,3);
print("OK") if (grep { $_ eq $titi} PDL::Matrix->pdl(\@toto));

None works.

Any help Please


Solution

  • Disclaimer: I don't know anything about PDL. I've read the source to figure this one out.

    There's a function PDL::all() that you can use in conjunction with the overloaded comparison operator ==.

    use PDL;
    my $foo = pdl(1,2,3);
    my $bar = pdl(4,5,6);
    my $qrr = pdl(1,2,3);
    
    print "OK 1" if PDL::all( $foo == $bar );
    print "OK 2" if PDL::all( $foo == $qrr );
    

    I'm still looking for the documentation.