Numpy数组的算术运算

时间:2014-04-05 15:06:19

标签: python arrays numpy

我有两个numpy数组pcarout_list

[[  5.80084178e-05   1.20779787e-02  -2.65970238e-02]
 [ -1.36810406e-02   6.85722519e-02  -2.60280724e-01]
 [  4.21996519e-01  -1.43644036e-01   2.12904690e-01]
 [  3.03098198e-02   1.50170659e-02  -1.09683402e-01]
 [ -1.50776089e-03   7.22369575e-03  -3.71181228e-02]
 [ -3.04448275e-01  -3.66987035e-01   1.44618682e-01]
 [ -1.46744916e-01   3.47112167e-01   3.09550267e-01]
 [  1.16567762e-03   1.72858807e-02  -9.39297514e-02]
 [  1.25896836e-04   1.61310167e-02  -6.00253128e-02]
 [  1.65062798e-02   1.96933143e-02  -4.26540031e-02]
 [ -3.78020965e-03   7.51770012e-03  -3.67852984e-02]]

[[-0.01368104  0.06857225 -0.26028072]
 [ 0.42199652 -0.14364404  0.21290469]]

我想从out_list中减去pcar,并希望得到两个不同的数组。

示例:

First Array as -

[[ 5   8    5]
 [ 1   2    4]
 [ 7   6    1]]

和第二个数组为 -

[[ 1   6   2]
 [ 4   5   3]]

那么结果应该是

[[ 4   2   3]
 [ 0   -4   2]
 [ 6    0  -1]]

[[ 1   3   2]
 [-3  -3   1]
 [ 3   1  -2]]

1 个答案:

答案 0 :(得分:1)

这应该做的工作:

import numpy as np
a = np.array([[1,2,3],[10,20,30],[100,200,300]])
b = np.array([[1,2,3],[10,20,30]])
result_list = [np.subtract(a,b_x) for b_x in b]
#now you can use the result_list:
print result_list[0]
#>>> [[  0   0   0]
#     [  9  18  27]
#     [ 99 198 297]]

print result_list[1]
#>>>[[ -9 -18 -27]
#    [  0   0   0]
#    [ 90 180 270]]