0

I have two identical arrays of size (2,3,2)

[[[1, 7],
  [2, 8], 
  [3, 9]],

 [[4, 10],
  [5, 11],
  [6, 12]]]

stored in a multidimensional array

>>> a  =  np.array([[[[ 1,  1],
              [ 7,  7]],

             [[ 2,  2],
              [ 8,  8]],

             [[ 3,  3],
              [ 9,  9]]],

            [[[ 4,  4],
              [10, 10]],

             [[ 5,  5],
              [11, 11]],

             [[ 6,  6],
              [12, 12]]]])

>>> a.shape
(2, 3, 2, 2)

I'm trying to mask the sub arrays in a with m:

>>> m  =  np.array([[[1, 0],
                 [0, 0],
                 [1, 1]],

                [[1, 1],
                 [0, 1],
                 [0, 0]]])

which should result in:

[[[[ 1,  1],
   [ 0,  0]

   [ 0,  0],
   [ 0,  0],

   [ 3,  3],
   [ 9,  9]]

  [[ 4, 4],
   [10, 10],

   [ 0,  0],
   [11,  11],

   [ 0,  0],
   [ 0,  0]]]

I tried to use np.concatenate and np.append eg, np.prod(np.concatenate([a,m],axis=0))

but none of my solutions worked.

1 Answer 1

2

Expand the dimensions of m so it broadcasts with a:

In [183]: a*m[...,None]                                                         
Out[183]: 
array([[[[ 1,  1],
         [ 0,  0]],

        [[ 0,  0],
         [ 0,  0]],

        [[ 3,  3],
         [ 9,  9]]],


       [[[ 4,  4],
         [10, 10]],

        [[ 0,  0],
         [11, 11]],

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