perlpdl

Best practice for accessing data in PDL objects


I have a 2 dimensional pdl,for example:

my $data  = random(4,4);

and I want to compute the sum of the 0,0 and the 1,0 element outside of the pdl context. (Not like, for example $data->slice('0,0') + $data->slice('1,0), which still returns a pdl object) What is the correct way of doing this?


Solution

  • If you're looking to extract individual elements as normal Perl scalars, use at instead of slice:

    pdl> $pdl = random(4,4)
    pdl> p $pdl->at(0,0) + $pdl->at(1,0)
    0.288796754210711
    pdl> p ref \($pdl->at(0,0) + $pdl->at(1,0))
    SCALAR
    

    To convert the entire ndarray object into nested Perl arrays, use unpdl

    pdl> $aoa = random(4,4)->unpdl;
    pdl> p ref $aoa
    ARRAY
    pdl> p ref $aoa->[0]
    ARRAY
    

    Note that the indexing of elements in the Perl arrays is the transverse of that done in the ndarray objects. For example,

    pdl> p $pdl->at(1,0)
    0.111869023064209
    
    pdl> p $aoa->[1][0] # wrong
    0.954887281829823
    
    pdl> p $aoa->[0][1] # correct
    0.111869023064209