简体   繁体   中英

how to return the 3rd elements of a numpy array if a condition is met?

Helloo, I have a numpy array of (x,y,z) coordinates.

coors = [[0,0,7], [1,1,6], [2,2,6], [3,3,6], [4,4,6], [5,5,6]] 

now I want to get all the 3rd element (z) of each coordinate if the (x,y) is inside a given rectangle

rect_mins = 2000,1800   # x_min, y_min
rect_maxs = 2100,1900 # x_max, y_max

coors [coors [:, 0] > 2000 and coors [:, 0] < 2100 and coors [:, 1] > 1800 and coors [:, 1] < 1900]

but I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How can I get rid of this error, also what is the most efficient way of doing this operation?

替换(仅检查一个语句为真)与&(执行按位操作),并添加括号(以分离每个操作)

coors [(coors [:, 0] > 2000) & (coors [:, 0] < 2100) & (coors [:, 1] > 1800) & (coors [:, 1] < 1900)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM