Q:

Write a NumPy program to calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to a given array

0

Write a NumPy program to calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to a given array.

All Answers

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

import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Original array: ")
print(x)
r1 = np.ediff1d(x, to_begin=[0, 0], to_end=[200])
r2 = np.insert(np.append(np.diff(x), 200), 0, [0, 0])
assert np.array_equiv(r1, r2)
print("Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:")
print(r2)

Sample Output:

Original array: 
[1 3 5 7 0]
Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:
[  0   0   2   2   2  -7 200]

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