10

If I have an d dimensional np.array, how can I get the indicies of the boundary?

For example, in 2d,

test = np.arange(16).reshape((4, 4))
test
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

Now I would like to get the boundaries

array([[ True,  True,   True,  True],
       [ True,  False,  False, True],
       [ True,  False,  False, True],
       [ True,  True,   True,  True]])

Great if efficient and works for arbitrary number of dimensions, but it has to work at least 3. The array is not a necessarily a hypercube, but potentially a hyperrectangle: the number of grid points in all dimension are not necessarily the same, unlike in the example.

For an array of shape (4, 5, 6), the expected output is

array([[[ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True]],
       [[ True,  True,  True,  True,  True,  True],
        [ True, False, False, False, False,  True],
        [ True, False, False, False, False,  True],
        [ True, False, False, False, False,  True],
        [ True,  True,  True,  True,  True,  True]],
       [[ True,  True,  True,  True,  True,  True],
        [ True, False, False, False, False,  True],
        [ True, False, False, False, False,  True],
        [ True, False, False, False, False,  True],
        [ True,  True,  True,  True,  True,  True]],
       [[ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True]]], dtype=bool)
3
  • "the number of grid points in each dimension are not necessarily the same" then it's not an NumPy array, right? Commented Jan 4, 2018 at 14:15
  • 1
    @jdehesa Is it clearer now? What I mean is that np.arange(20).reshape((4, 5)) is also a potential use case. Commented Jan 4, 2018 at 14:17
  • Ah sorry I understand now, I thought you meant some kind of jagged array or something. Commented Jan 4, 2018 at 14:18

1 Answer 1

13

You could do this by constructing a tuple of slices, e.g.

import numpy as np

def edge_mask(x):
    mask = np.ones(x.shape, dtype=bool)
    mask[x.ndim * (slice(1, -1),)] = False
    return mask

x = np.random.rand(4, 5)
edge_mask(x)

# array([[ True,  True,  True,  True,  True],
#        [ True, False, False, False,  True],
#        [ True, False, False, False,  True],
#        [ True,  True,  True,  True,  True]], dtype=bool)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.