Selecting a part of a 2d array of data depending on two 2d arrays

Question:

I have three arrays x, y, data, with:

print(x.shape, y.shape, data.shape)

(565, 1215) (565, 1215) (565, 1215)

whereby:

print(x.min(), y.min(), data.min(), x.max(), y.max(), data.max())

-55.530094 33.582264 0.0 55.530094 66.823235 275.67851091467816

How can I select values from 2d array data where ((x>=-20) & (x<=20) & (y>=35) & (y<=60))?

I tried the following:

indices = np.where((x>=-20) &  (x<=20) & (y>=35) &  (y<=60))

print(indices)

(array([ 28,  28,  28, ..., 540, 540, 540], dtype=int64), array([ 35,  36,  37, ..., 671, 672, 673], dtype=int64))

How can I apply this indices to data?

Asked By: len

||

Answers:

Simply data[indices]

Example:

import numpy as np

x = np.random.uniform(0, 100, size=(100, 100))
y = np.random.uniform(0, 100, size=(100, 100))
data = np.random.randint(0, 1000, size=(100, 100))

indices = np.where((x >= -20) & (x <= 20) & (y >= 35) & (y <= 60))
print(data[indices])

Answered By: u1234x1234
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.