1

I would like to apply a mask all around the edges of an array, as example in a 3x3 array :

0 0 0
0 1 0
0 0 0

In stack i found this command, but i can t apply a second condition to get my specific array...

import numpy as np
np.logical_and.outer(np.arange(3) >= 2, np.arange(3) >= 2)

I get that :

0 0 0
0 0 0 
0 0 1

2 Answers 2

1

You could always construct a mask to deselect the first and last rows/columns like this:

>>> mask = np.ones((3, 3), dtype=bool)
>>> mask 
array([[True, True, True],
       [True, True, True],
       [True, True, True]], dtype=bool)

>>> mask[0], mask[-1], mask[:,0], mask[:,-1] = False, False, False, False
>>> mask
array([[ False,  False,  False],
       [ False,  True,   False],
       [ False,  False,  False]], dtype=bool)
Sign up to request clarification or add additional context in comments.

Comments

1

You can construct the 2-d mask directly from a 1-d mask:

In [6]: np.logical_and.outer([0,1,0],[0,1,0])
Out[6]:
array([[False, False, False],
       [False,  True, False],
       [False, False, False]], dtype=bool)

EDIT: For the generic case you can do something like:

In [11]: np.logical_and.outer([0]+[1]*3+[0],[0]+[1]*5+[0]).astype(int)
Out[11]:
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 1, 1, 1, 1, 1, 0],
       [0, 0, 0, 0, 0, 0, 0]])

1 Comment

Thanks! ;) But my array is 2092x1936 ^^ and i would like to suppress 100 values at each edge part so i cant do manually..

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.