[英]Rearranging 2 arrays with different lengths to create a third array Numpy Python
我在a
和b
下方有 2 个数组组合来生成result
。 a
乘以a_multiplier
, b
乘以b_multiplier
。 a
和b
有不同长度的数组,我想将它们组合和相乘,然后按a*a_multiplier, b*b_multiplier,a*a_multiplier.....
顺序重新排列它们。 我怎样才能修改result
函数以便预期输出起作用?
import numpy as np
a_multiplier = 3
b_multiplier = 5
a = np.array([5,32,1,4,3])
b = np.array([1,5,11,3])
result = np.vstack([a * a_multiplier, b * b_multiplier]).flatten("F")
预期输出:
[15 5 96 25 3 55 12 15 9]
值错误:
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 4
干得好:
import numpy as np
a_multiplier = 3
b_multiplier = 5
a = np.array([5,32,1,4,3])
b = np.array([1,5,11,3])
result = np.empty((a.size + b.size,), dtype=a.dtype)
result[0::2] = a * a_multiplier
result[1::2] = b * b_multiplier
输出:
array([15, 5, 96, 25, 3, 55, 12, 15, 9])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.