简体   繁体   English

如何获取numpy掩码数组的有效值的索引

[英]How to get indices of valid values of numpy masked array

I have a netCDF4 dataset representing multiple matrices of the same dimension (551, 146), one matrix ( M1 ) contains longitude values, another matrix ( M2 ) contains latitude values. 我有一个netCDF4数据集,该数据集表示相同维度的多个矩阵(551,146),一个矩阵( M1 )包含经度值,另一个矩阵( M2 )包含纬度值。 Each matrix is a numpy masked array. 每个矩阵都是一个numpy掩码数组。

Given a lon/lat tuple, (A, B) , I want to get the matrix indices ( lon, lat ) where value A matches in M1 and value B matches in M2 . 给定一个lon / lat元组(A, B) ,我想获取矩阵索引( lon, lat ),其中值AM1匹配,值BM2匹配。

I thought I could represent the indices with: 我以为可以用以下方式表示索引:

lon_idx, lat_idx = np.mgrid[:lon.shape[0], :lon.shape[1]]

and two matrices of same shape where one have all values set to A and the other have all values set to B . 以及两个形状相同的矩阵,其中一个的所有值都设置为A ,另一个的所有值都设置为B

Then I am hoping to somehow combine these matrices and end up with an array of lon , lat indices where the values matched. 然后,我希望以某种方式组合这些矩阵,并最终得到一个值匹配的lonlat索引数组。

What is the idiomatic way to do this in numpy? 在numpy中这样做的惯用方式是什么?

Thanks to SnoopJeDi on #python @freenode who found the solution for me: 感谢#python @freenode上的SnoopJeDi为我找到了解决方案:

In [58]: np.argwhere((lon == lon[250][145]) & (lat == lat[250][145]))
Out[58]: array([[250, 145]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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