Suppose I have:
$a = [
[1, 0, 1]
[0, 1, 0]
[0, 1, 1]
]
and I want to extract all rows where $row[2] == 1
. My resulting piddle would look like:
$b = [
[1, 0, 1]
[0, 1, 1]
]
Is this possible with PDL?
Suppose I have:
$a = [
[1, 0, 1]
[0, 1, 0]
[0, 1, 1]
]
and I want to extract all rows where $row[2] == 1
. My resulting piddle would look like:
$b = [
[1, 0, 1]
[0, 1, 1]
]
Is this possible with PDL?
You need to use which
to generate a list of indexes of your matrix which have a value of 1 in the third column
which($aa->index(2) == 1)
and pass this to dice_axis
, which will select the rows with the given indexes. Axis 0 is the columns and axis 1 is the rows, so the code looks like this
use strict;
use warnings 'all';
use PDL;
my $aa = pdl <<__END_PDL__;
[
[1, 0, 1]
[0, 1, 0]
[0, 1, 1]
]
__END_PDL__
my $result = $aa->dice_axis(1, which($aa->index(2) == 1));
print $result;
[
[1 0 1]
[0 1 1]
]
I'm new to PDL
, but it seems like you can use which
result as a mask.
You need to transpose
original variable first, then transpose
it back after using slice
.
pdl> $a = pdl [[1, 0, 1], [0, 1, 0], [0, 1, 1]]
pdl> p which($a(2) == 1)
[0 2]
pdl> p $a->transpose
[
[1 0 0]
[0 1 1]
[1 0 1]
]
pdl> p $a->transpose->slice(which($a(2) == 1))->transpose
[
[1 0 1]
[0 1 1]
]