Search code examples
python-2.7numpyarcpy

How to apply a formula to each cell of a numpy array


I am trying to take an existing numpy array and apply a formula to each cell of the array. I have the code below but it returns the following error. Traceback (most recent call last): File "C:\gTemp\Text-1.py", line 5, in myarray = 0.1236 * math.tan(myarray / 2842.5 + 1.1863) TypeError: only length-1 arrays can be converted to Python scalars

I am new to numpy and I am looking for skill level appropriate advice. Here is my existing code.

import arcpy
import numpy
import math
myarray =  numpy.load(r"E:\depthtester2.npy")
myarray = 0.1236 * math.tan(myarray / 2842.5 + 1.1863)
myRaster = arcpy.NumPyArrayToRaster(myarray,arcpy.Point(0.0,0.0),1.0, 1.0, -99999.0 )
myRaster.save("E:\deptht")
print "done"

Solution

  • Instead of math.tan(), use numpy.tan(). The numpy functions are designed to work elementwise on numpy arrays.