- #1
ver_mathstats
- 260
- 21
- Homework Statement
- Write a Python code to create the function array_change(a, new_val) that has two arguments:
a that is a NumPy array whose entries are numbers, and
new_val which is a number. The function returns a new array after changing every occurrence of entries in
a, that are in absolute value strictly less than 1, to new_val.
- Relevant Equations
- Python
Code:
import numpy as np
def array_change(a,new_val):
a=np.array([])
for i in range(a):
if abs(i)<1:
a[i]=new_val
e=np.arange(-2, 2, 0.2).reshape(4,5)
print(array_change(e,0))
I am not sure where I am going wrong exactly but I keep getting an error message.
I came up with a code that gives me the results I am looking for but it is not a function.
Code:
e=np.arange(-2, 2, 0.2).reshape(4,5)
c=abs(e)<1
e[c]=0
print(e)
Any help is appreciated. Thank you.