Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for accessing data in PDL objects

Tags:

perl

pdl

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?

like image 867
Indiano Avatar asked Oct 29 '25 09:10

Indiano


1 Answers

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
like image 141
Diab Jerius Avatar answered Oct 31 '25 14:10

Diab Jerius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!