We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★) hint: array[::4] # Author: Warren Weckesser Z = np.array([1,2,3,4,5]) nz = 3 Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz)) Z0[::nz+1] = Z print(Z0)
# Author: Warren Weckesser Z = np.array([1,2,3,4,5]) nz = 3 Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz)) Z0[::nz+1] = Z print(Z0)
A more readable function from numpy: numpy.insert(arr, obj, values, axis=None). NumPy Doc
numpy.insert(arr, obj, values, axis=None)
Thus, an alternative solution will be:
Z = np.array([1, 2, 3, 4, 5]) Z0 = np.insert(a[..., np.newaxis], [1], [[0, 0, 0] for _ in range(len(a))], axis=1) Z0 = Z0.flatten()[:-3] print(Z0)
The text was updated successfully, but these errors were encountered:
Sorry, something went wrong.
@Jeff1999 Thansk. Did you time the two solutions ? I'm not too confident with the for loop and a big array.
No branches or pull requests
A more readable function from numpy:
numpy.insert(arr, obj, values, axis=None)
. NumPy DocThus, an alternative solution will be:
The text was updated successfully, but these errors were encountered: