at#

Return a single element of an ndarray as a plain Perl scalar.

@pos is a coordinate list whose length matches the number of dimensions of $pdl. Indices may be negative (counted from the far end, so -1 is the last element along that axis). Missing trailing indices default to 0; extra trailing indices past $pdl->ndims are ignored.

The value you get back is current even if $pdl is a view onto another ndarray that has since been written to. Integer-typed ndarrays give you an integer, float-typed ones a float.

Signature#

$z = at($pdl, @pos);       # function form
$z = $pdl->at(@pos);       # method form

Broadcasting#

at does not broadcast. It is a scalar-valued accessor: supply exactly one coordinate tuple.

Bad values#

If $pdl has its bad-value flag set and the element fetched is the bad value, at returns the string "BAD". With the flag clear the raw number is returned regardless of what it happens to be.

Examples#

use PDL;
my $x = sequence(3, 4);     # 3x4, values 0..11
print $x->at(1, 2);         # 7
print $x->at(-1, -1);       # 11 (last element via negative indexing)

# BAD value propagation
my $b = pdl([1, 2, 3]);
$b->badflag(1);
$b->inplace->setbadat(1);
print $b->at(1);            # "BAD"

Errors#

Croaks with "Position out of range" when:

  • $pdl is a null ndarray, fresh from PDL->null and not yet given dimensions;

  • $pdl has zero elements, as pdl([]) does;

  • any supplied index, after negative indices are resolved, falls outside [0, dim_size) for its axis.