Write a NumPy program to create a new vector with 2 consecutive 0 between two values of a given vector.
import numpy as np nums = np.array([1,2,3,4,5,6,7,8]) print("Original array:") print(nums) p = 2 new_nums = np.zeros(len(nums) + (len(nums)-1)*(p)) new_nums[::p+1] = nums print("\nNew array:") print(new_nums)
Sample Output:
Original array: [1 2 3 4 5 6 7 8] New array: [1. 0. 0. 2. 0. 0. 3. 0. 0. 4. 0. 0. 5. 0. 0. 6. 0. 0. 7. 0. 0. 8.]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer