0

I basically have 2 arrays, one containing Lattitude values, one Longitude. What I want is to extract those that meet a certain requirement .

xLong = np.extract(abs(Long-requirement)<0.005,Long)
xLat = np.extract(abs(Lat-requirement)<0.005,Lat)

Lat and Long are numpy arrays.

However, I only want to get those coordinates that both lat/long meet the requirement and I'm not sure how to do it .

If it's possible, I need to use numpy functions since I'm looking for optimization as well. I know that I can iterate through all using a for and just add to different array but that would take a lot of time

1 Answer 1

1

You need to do this with boolean indexing. Whenever you create an boolean array the same shape as your array of interest, you can get just the True values out by indexing with the boolean array. I assume below that Long and Lat are the same size; if they're not the code will throw an exception.

# start building the boolean array.  long_ok and lat_ok will be the same
# shape as xLong and xLat.
long_ok = np.abs(Long - requirement) < 0.005
lat_ok = np.abs(Lat - requirement) < 0.005

# both_ok is still a boolean array which is True only where 
# long and lat are both in the region of interest
both_ok = long_ok & lat_ok

# now actually index into the original arrays.
long_final = Long[both_ok]
lat_final = Lat[both_ok]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, they are the same size. Thank you for the answer, code works nice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.