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

문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top