简体   繁体   English

python numpy减去矩阵元素

[英]python numpy subtract elements that are matrices

Using numpy, how can I subtract the elements of a numpy array that are matrices by each other? 使用numpy,我如何相互减去矩阵的numpy数组的元素?

a = np.array([ [ [1., 2., 3.],    [4., 5., 6.],    [7., 8., 9.]    ],
               [ [20., 21., 22.], [23., 24., 25.], [26., 27., 28.] ],
               [ [30., 31., 32.], [33., 34., 35.], [36., 37., 38.] ]
             ])

or in plain English: 或者用简单的英语:

a = ([matrix1], [matrix2], [matrix3])

I want to get a np.array that calculates as follows: 我想得到一个np.array计算如下:

[matrix1-matrix1][matrix1-matrix2][matrix1-matrix3]

[matrix2-matrix1][matrix2-matrix2][matrix2-matrix3]

[matrix3-matrix1][matrix3-matrix2][matrix3-matrix3]

Diagonal will be a matrix with zero values in the matrix. 对角线将是矩阵中具有零值的矩阵。

How can I accomplish this? 我怎么能做到这一点?

Your a is a 3d array 你的a是一个3d数组

In [258]: a = np.array([ [ [1., 2., 3.],    [4., 5., 6.],    [7., 8., 9.]    ],
   .....:                [ [20., 21., 22.], [23., 24., 25.], [26., 27., 28.] ],

   .....:                [ [30., 31., 32.], [33., 34., 35.], [36., 37., 38.] ]
   .....:              ])
In [259]: a.shape
Out[259]: (3, 3, 3)

2d arrays can be accessed with indexing 可以使用索引访问2d数组

In [260]: a[0,:,:]
Out[260]: 
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

and a tuple of 3 2d arrays can be obtained with: 并且可以获得3个2d数组的元组:

In [261]: (a[0,:,:],a[1,:,:],a[2,:,:])
Out[261]: 
(array([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.]]), 
 array([[ 20.,  21.,  22.],
        [ 23.,  24.,  25.],
        [ 26.,  27.,  28.]]), 
 array([[ 30.,  31.,  32.],
        [ 33.,  34.,  35.],
        [ 36.,  37.,  38.]]))

This is equivalent to your (matrix1, matrix2, matrix3) expression. 这相当于您的(matrix1, matrix2, matrix3)表达式。 I'm using 2d array instead of matrix because numpy has an array subclass called matrix , so the term can be confusing. 我使用的是2d array而不是matrix因为numpy有一个名为matrix的数组子类,因此该术语可能会令人困惑。

A way to generate your subarray cross-difference is with broadcasting. 生成子阵列交叉差异的一种方法是使用广播。 I use the None syntax to expand the dimension of a . 我用的是None语法扩大的尺寸a

In [262]: a[:,None,:,:]-a[None,:,:,:]
Out[262]: 
array([[[[  0.,   0.,   0.],
         [  0.,   0.,   0.],
         [  0.,   0.,   0.]],

        [[-19., -19., -19.],
         [-19., -19., -19.],
         [-19., -19., -19.]],

        ...

        [[ 10.,  10.,  10.],
         [ 10.,  10.,  10.],
         [ 10.,  10.,  10.]],

        [[  0.,   0.,   0.],
         [  0.,   0.,   0.],
         [  0.,   0.,   0.]]]])
In [263]: _.shape
Out[263]: (3, 3, 3, 3)

The shape of this new array is 4d, which can be thought of as a 3x3 array of 3x3 arrays. 这个新阵列的形状是4d,可以认为是3x3阵列的3x3阵列。 And yes, the diagonal elements A[i,i,:,:] are all 0s. 是的,对角元素A[i,i,:,:]都是0。

In [264]: A=a[:,None,:,:]-a[None,:,:,:]
In [265]: A[2,2,:,:]
Out[265]: 
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM