use warnings; use strict; use PDL; use PDL::NiceSlice; #fancy matrix #set to a blank 3d test matrix my $matrix = zeroes 7,7,3; #set some values for matrix $matrix( pdl( 0..2, 4..6 ), 4, 0:2 ) .= 1; $matrix( 4, pdl( 0..2, 4..6 ), 0:2 ) .= 1; #the conditional problem child my $ind = $matrix-> whichND( $matrix == 1 ); if ( all $matrix-> indexND( $ind ) == 1 ) { print "it worked\n"; } #### use warnings; use strict; use PDL; use PDL::NiceSlice; use Test::PDL 'eq_pdl', -tolerance => 0.42; my $mask = zeroes 7,7,3; $mask( pdl( 0..2, 4..6 ), 4, 0:2 ) .= 1; $mask( 4, pdl( 0..2, 4..6 ), 0:2 ) .= 1; my $good_matrix = random 7,7,3; my $some_matrix; my $count; while () { $some_matrix = random 7,7,3; last if eq_pdl $good_matrix * $mask, $some_matrix * $mask; $count ++ } $PDL::doubleformat = '%.1f'; print "it worked only after $count attempts,\n", "mask was: $mask", "good matrix was: $good_matrix", "and it was not very different from: $some_matrix"; #### it worked only after 144532 attempts, mask was: [ [ [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] [0 0 0 0 0 0 0] [1 1 1 0 1 1 1] [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] ] [ [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] [0 0 0 0 0 0 0] [1 1 1 0 1 1 1] [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] ] [ [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] [0 0 0 0 0 0 0] [1 1 1 0 1 1 1] [0 0 0 0 1 0 0] [0 0 0 0 1 0 0] ] ] good matrix was: [ [ [0.4 0.3 0.3 0.9 0.6 0.3 0.2] [0.8 0.8 0.2 0.8 0.6 0.9 0.2] [0.9 0.0 1.0 0.3 0.8 1.0 0.3] [0.5 0.3 0.8 0.5 0.3 0.6 1.0] [0.8 0.7 0.3 0.4 0.3 0.2 0.4] [1.0 0.4 0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