# x[:,:,None,:]-x[:,:,:,None]

09/18 15:28

### x[:,:,None,:]-x[:,:,:,None]

None相当于在数组中多加一个维度。

``````x = np.arange(24).reshape((2,3,4))
``````

``````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]]])
``````
`````` x[:,:,None,:]
``````

``````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]]]])
``````
``````x[:,:,:,None]
``````

``````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]]]])
``````
``````x[:,:,None,:]-x[:,:,:,None]
``````

``````array([[[[ 0,  1,  2,  3],
[-1,  0,  1,  2],
[-2, -1,  0,  1],
[-3, -2, -1,  0]],

[[ 0,  1,  2,  3],
[-1,  0,  1,  2],
[-2, -1,  0,  1],
[-3, -2, -1,  0]],

[[ 0,  1,  2,  3],
[-1,  0,  1,  2],
[-2, -1,  0,  1],
[-3, -2, -1,  0]]],

[[[ 0,  1,  2,  3],
[-1,  0,  1,  2],
[-2, -1,  0,  1],
[-3, -2, -1,  0]],

[[ 0,  1,  2,  3],
[-1,  0,  1,  2],
[-2, -1,  0,  1],
[-3, -2, -1,  0]],

[[ 0,  1,  2,  3],
[-1,  0,  1,  2],
[-2, -1,  0,  1],
[-3, -2, -1,  0]]]])
``````

0
0 收藏

0 评论
0 收藏
0