I have a default numpy array (speed, pressure or temperature data) like this:
a=[[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]
[40. 41. 42. 43. 44. 45. 46. 47. nan 49.]]
I need to apply the following conditions and then use the corresponding formula
a<5 (a*5)+4
a>5 (a**2.)-2
I tried using:
a[a<5]=(a[a<5]*5.)+4.
but it does not work and I have also used the method creating Boolean matrices and then multiplying them by the formulas corresponding to the condition, like this:
les=(a<5.).astype(float)
mayor=(a>5.).astype(float)
les=les*((a*5)+4)
mayor=mayor*((a**2.)-2)
b=les+mayor
This works but it uses many lines of code and I think it is impractical and I would like to know if there is an easier way to do this.
np.whereshould also work.np.where(a<5, (a*5)+4, (a**2)-2)a = 5? Should the value 5 be kept?