How to perform basic math on numpy ndarray

Question:

So, I have a numpy ndarray with dimensions (984, 1977, 2). What I want to accomplish is to have a numpy ndarray where I do basic math on the final values. So let’s say data is my ndarray. And data[0][0] equals to [72 46]. So I want to perform (72 – 46) / (72 + 46) and store that value in my new ndarray for each of the pair. Basically, it represents two stacked bands and the final output needs to be a resultant of the two based on the above formula (not NDVI). I have been looking at numpy’s tutorial to find an answer but no luck so far.

Asked By: Matija

||

Answers:

For that array

x = data[:,:,0]
y = data[:,:,1]

are the two ‘last’ column values

res = (x-y)/(x+y)

should be the result you want for all pairs, a (984, 1977) shape array.

Answered By: hpaulj