Multiply two (ore more) arrays seperately with one factor in a loop - return two seperate arrays

didadiluo 注册会员
2023-01-25 16:36

use numpy.multiply for this

x = np.array([2,3,4])
y = np.multiply(x, 1e5)


[200000. 300000. 400000.]

x is not changed in the process

duoqingduoyi 注册会员
2023-01-25 16:36
def arrays(a1, a2, factor):
    return a1*factor, a2*factor
a1, a2 = multiply_two_arrays(p1, p2, 10)
cxcmvpmtv 注册会员
2023-01-25 16:36

NumPy arrays support scalar multiplication (it's a special case of broadcasting). Just directly multiply the array by the constant: p1 *= 1e5

If you get a UFuncTypeError, it means that your array datatype doesn't match the type of the constant multiplier. For example a = np.array([1,2,3]) will create an array with int32 datatype by default, and NumPy casting rules don't allow it to by multiplied by a float. To fix this, you can explicitly specify the datatype: a = np.array([1,2,3], dtype=float) or you can give the entries as floats: a = np.array([1.0,2.0,3.0])

About the Author

Question Info

Publish Time
2023-01-25 16:36
Update Time
2023-01-25 16:36