I have 2 single-dimensional NumPy arrays
a = np.array([0, 4])
b = np.array([3, 2])
I want to create a 2d array of numbers
c = np.array([[0,1,2], [4,5]])
I can also create this using a for loop
EDIT: Updating loop based on @jtwalters comments
c = np.zeros(b.shape[0], dtype=object)
for i in range(b.shape[0]):
c[i] = np.arange(a[i], a[i]+b[i])
How can I achieve this via vectorization/broadcasting?
[[0,1,2], [4,5]]cannot be made into a 2d array.np.arange(6).reshape(2,3)work for you?np.arange(6).reshape(2,3)won't work. 2 rows should benp.arange(a[i], a[i]+b[i])please note that3is not in my resulting array.