2

I have a numpy array like this:

>>> I
array([[ 1.,  0.,  2.,  1.,  0.],
       [ 0.,  2.,  1.,  0.,  2.]])

And an array A like this:

>>> A = np.ones((2,5,3))

I'd like to obtain the following matrix:

>>> result
array([[[ False,  False,  True],
        [ False,  True,  True],
        [ False,  False,  False],
        [ False,  False,  True],
        [ False,  True,  True]],

       [[ False,  True,  True],
        [ False,  False,  False],
        [ False,  False,  True],
        [ False,  True,  True],
        [ False,  False,  False]]], dtype=bool)

It is better to explain with an example:
I[0,0] = 1 -> result[0,0,:2] = False and result[1,1,2:] = True
I[1,0] = 0 -> result[1,1,0] = False and result[1,1,1:] = True

Here is my current implementation (correct):

result = np.empty((A.shape[0], A.shape[1], A.shape[2]))
r = np.arange(A.shape[2])
for i in xrange(A.shape[0]):
    result[i] = r > np.vstack(I[i])

print result.astype(np.bool)

Is there a way to implement in a faster way (avoiding the for loop)?

Thanks!

2
  • I tried to run your code, but it does not work for me. Could you revise your question and make it clearer? Commented Oct 27, 2014 at 7:24
  • Sorry, my fault. A is a (2,5,3) matrix. I added also a print with the bool conversion Commented Oct 27, 2014 at 7:29

1 Answer 1

1

You just need to add another dimension on to I, such that you can broadcast r properly:

result = r > I.reshape(I.shape[0],I.shape[1],1)

e.g.

In [41]: r>I.reshape(2,5,1)
Out[41]: 
array([[[False, False,  True],
        [False,  True,  True],
        [False, False, False],
        [False, False,  True],
        [False,  True,  True]],

       [[False,  True,  True],
        [False, False, False],
        [False, False,  True],
        [False,  True,  True],
        [False, False, False]]], 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.