1

x is an array of shape(n_dim,n_row,n_col) of 1st n natural numbers b is boolean array of shape(2,) having elements True,false


    def array_slice(n,n_dim,n_row,n_col):
             x = np.arange(0,n).reshape(n_dim,n_row,n_col)
             b = np.full((2,),True)
             print(x[b])
             print(x[b,:,1:3]) 

expected output

[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]]]
[[[ 1  2]
  [ 6  7]
  [11 12]]]

my output:-

[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]]
 [[15 16 17 18 19]
  [20 21 22 23 24]
  [25 26 27 28 29]]]
[[[ 1  2]
  [ 6  7]
  [11 12]]
 [[16 17]
  [21 22]
  [26 27]]]
hpaulj
  • 221,503
  • 14
  • 230
  • 353
Chandler
  • 15
  • 3
  • Could you please add the function caller to your question to know which values you set for parameters? – Hamzah May 26 '22 at 08:27
  • if __name__ == '__main__': n = int(input()) n_dim = int(input()) n_row = int(input()) n_col = int(input()) array_slice(n,n_dim,n_row,n_col) – Chandler May 26 '22 at 09:50
  • `b` does not have a `False` element, which is why you are not getting the expected output. – Professor Pantsless May 26 '22 at 16:49
  • Code does not post well in a comment - edit your question. And `input` does not help us easily recreate your case. You could have just given something like `array_slice(1,2,3)`. Try to imagine what's it's like to be someone else looking at this code. What would you want to know? – hpaulj May 26 '22 at 17:11

1 Answers1

0

An example:

In [83]: x= np.arange(24).reshape(2,3,4)

In [84]: b = np.full((2,),True)

In [85]: x
Out[85]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

In [86]: b
Out[86]: array([ True,  True])

With two True, b selects both plains of the 1st dimension:

In [87]: x[b]
Out[87]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

A b with a mix of true and false:

In [88]: b = np.array([True, False])

In [89]: b
Out[89]: array([ True, False])

In [90]: x[b]
Out[90]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]]])
hpaulj
  • 221,503
  • 14
  • 230
  • 353