PDL::Slices#
Take pieces of an ndarray, and rearrange the pieces you take.
use PDL;
my $m = sequence(4, 3);
print $m->slice("1:2,(0)"), " ", $m->slice(":,(-1)"), "\n"; # [1 2] [8 9 10 11]
print $m->dice([0, 3], [2])->flat, " ", $m->xchg(0, 1)->dims, "\n"; # [8 11] 34
print sequence(5)->index(pdl(4, 0)), " ", sequence(5)->rotate(1), "\n"; # [4 0] [4 0 1 2 3]
my $row = $m->slice(":,1");
$row .= 0;
print $m->sum, " ", $m->clump(-1)->dims, "\n"; # 44 12
my ($len, $val) = rle(pdl(5, 5, 7));
print "$len $val ", rld($len, $val), "\n"; # [2 1] [5 7] [5 5 7]
Most of these return a view, not a copy: the result shares storage with the ndarray it came from, so writing into the view writes into the original - $row .= 0 above zeroes a row of $m. Call sever or copy when the two should part company.
slice takes, per dimension, a range "start:end:step", a single index in parentheses "(i)" (which drops the dimension) or nothing for all; negative indices count from the end. dice and dice_axis pick elements by number, index, index1d and index2d by an index ndarray, range gathers neighbourhoods around coordinates (and indexND, one element at each), diagonal takes a diagonal. The rest rearrange without changing values: xchg, mv, reorder, flat, clump, splitdim, rotate, lags, and broadcast/unbroadcast for broadcasting control. rle/rld and their vec, seq and ND forms encode and decode runs.
This is PDL::Slices of PDL 2.106. The kernels are PDL’s own, through pperl’s engine; upstream’s Perl is used unchanged for slice, dice, dice_axis, diagonal, reorder, meshgrid, using, rle, rld, rleND, rldND, rldvec, rldseq, indexND and indexNDb.