Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Each matrix is a numpy masked array.

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.

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.

Then I am hoping to somehow combine these matrices and end up with an array of lon, lat indices where the values matched.

What is the idiomatic way to do this in numpy?

like image 533
jollyroger Avatar asked Oct 27 '25 09:10

jollyroger


1 Answers

Thanks to SnoopJeDi on #python @freenode who found the solution for me:

In [58]: np.argwhere((lon == lon[250][145]) & (lat == lat[250][145]))
Out[58]: array([[250, 145]])
like image 142
jollyroger Avatar answered Oct 30 '25 00:10

jollyroger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!