Borys
Borys

Reputation: 1423

Arithmetic within numpy arrays

I have read 3 raster images of equal shape (500 by 500) as numpy array, and have put them in this way:

rasters = np.array(A,B,C)

Where A, B, C are 2d numpy arrays belonging to each image.

Now I have to calculate the followings:

result1 = B-A
result2 = C-B

Then,

final_result = np.max([result1,result2],axis = 0)

The final_result should have the same shape of A or B or C (i.e., 500 by 500)

How can I do it?

Upvotes: 1

Views: 69

Answers (2)

Jaime
Jaime

Reputation: 67427

You can use np.diff and np.max:

np.max(np.diff(rasters, axis=0), axis=0)

Alternatively:

np.max(rasters[1:] - rasters[:-1], axis=0)

Upvotes: 3

Scott Hunter
Scott Hunter

Reputation: 49803

B-A is accomplished using np.subtract(B,A).

Upvotes: 1

Related Questions