1

I have numpy arrays of shape (600,600,3), where the values are [-1.0, 1.0]. I would like to expand the array to (600,600,6), where the original values are split into the amounts above and below 0. Some examples (1,1,3) arrays, where th function foo() does the trick:

>>> a = [-0.5, 0.2, 0.9]
>>> foo(a)
[0.0, 0.5, 0.2, 0.0, 0.9, 0.0]  # [positive component, negative component, ...]
>>> b = [1.0, 0.0, -0.3]  # notice the behavior of 0.0
>>> foo(b)
[1.0, 0.0, 0.0, 0.0, 0.0, 0.3]

1 Answer 1

3

Use slicing to assign the min/max to different parts of the output array

In [33]: a = np.around(np.random.random((2,2,3))-0.5, 1)

In [34]: a
Out[34]: 
array([[[-0.1,  0.3,  0.3],
        [ 0.3, -0.2, -0.1]],

       [[-0. , -0.2,  0.3],
        [-0.1, -0. ,  0.1]]])

In [35]: out = np.zeros((2,2,6))

In [36]: out[:,:,::2] = np.maximum(a, 0)

In [37]: out[:,:,1::2] = np.maximum(-a, 0)

In [38]: out
Out[38]: 
array([[[ 0. ,  0.1,  0.3,  0. ,  0.3,  0. ],
        [ 0.3,  0. ,  0. ,  0.2,  0. ,  0.1]],

       [[-0. ,  0. ,  0. ,  0.2,  0.3,  0. ],
        [ 0. ,  0.1, -0. ,  0. ,  0.1,  0. ]]])
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.