Q:

Write a NumPy program to create a new vector with 2 consecutive 0 between two values of a given vector

0

Write a NumPy program to create a new vector with 2 consecutive 0 between two values of a given vector.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now