How to search in one NumPy array for positions for getting at these position the value from a second NumPy array?

StackOverflow https://stackoverflow.com/questions/22996507

Question

I have two raster files which I have converted into NumPy arrays (arcpy.RasterToNumpyArray) to work with the values in the raster cells with Python.

One of the raster has two values True and False. The other raster has different values in the range between 0 to 1000. Both rasters have exactly the same extent, so both NumPy arrays are build up identically (columns and rows), except the values.

My aim is to identify all positions in NumPy array A which have the value True. These positions shall be used for getting the value at these positions from NumPy array B.

Do you have any idea how I can implement this?

Was it helpful?

Solution

If I understand your description right, you should just be able to do B[A].

OTHER TIPS

You can use the array with True and False values to simply index into the other. Here's a sample:

import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([[True,False,False],[False,True,False],[False,False,True]])
a[b] ## gives  array([1, 5, 9])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top