How to iterate over Perl PDL?

The next i got something like

use PDL;
my $u = pdl [1,2,3,4];
my $dim = 4;
for(my $i=0; $i<$dim; $i++)
{
  print $u->flat->index($i), "\n";
}

Also, how can I convert [1,2,3,4]to piddle $u, can I return a list (or list of lists for a matrix) from $u?

+3
source share
2 answers

With wisdom from the monks, I found the answer: http://perlmonks.org/index.pl?node_id=892201

Thought I'd share this here in my original question. The above code can be rewritten as:

use PDL;
my $u = pdl [1,2,3,4];
foreach ($u->dog)
{
  print $_, "\n";
}

Wisdom came with a disclaimer that dog()only works on small armpits (objects).

+3
source

Also, using a dog, here are two other options for your 1d pdl, using an index and a list. there is also index2d

use PDL;
my $a = pdl(1 .. 4);
#use index
print $a->index($_), "\n" foreach (0 .. $a->nelem-1);
#use list
print $_ . "\n" foreach ($a->list);
+3
source

All Articles