# Set up initial 4x4 matrix with some arbitrary data pdl> p $b = pdl (10..25)->reshape(4,4); [ [10 11 12 13] [14 15 16 17] [18 19 20 21] [22 23 24 25] ] # Fetch the 2D index for the position of element with value 21 pdl> p $pos = whichND($b == 21); [ [3 2] ] # Simple transformation matrix to mark adjacent cells. pdl> $xfrm = pdl [-1,0],[0,-1],[1,0],[0,1]; # Show the results of the transformation: So far so good (even though # one of the values is out of bounds, we're expecting that.) pdl> p $pos + $xfrm; [ [2 2] [3 1] [4 2] [3 3] ] # Here's the rub. The following command returns the *values* of # the adjacent cells, but I want the indicies instead. (See discussion) pdl> p $b->indexND($pos + $xfrm, 'truncate'); [20 17 0 25] # $b->range($pos + $xfrm, 1, 'truncate') gives similar results #### pdl> p $indicies = $b->indexND($pos + $xfrm, 'truncate')->which($b % 2); # Returns: empty # # Expected: Should contain two single element piddles: # [17 25] # OR, the indicies in the original matrix would be fine: # [ # [3 1] # 17 # [3 3] # 25 # ] # N.b. [2 2] would be omitted (value = 20, even), as would [4 2] (index out of bounds)