user3035922
user3035922

Reputation:

Extract an array from a numpy array

Before I start sorry for my English, my poor python knowledge (newbie) and a possible duplicate question. I tried and searched a lot but couldn't find any solution to the problem that I got stuck. Here is the problem;

I have an array named array1 that is loaded with numpy.loadtxt() it is a text file that has 2 columns of data with x and y. x range from 0.4 to 15. the increment is not problem.

I also have a second array array2 which contains x' values range from 10 to 12.

Note: The increment of xin each array is different. I will use them for linear interpolation for y values later.

I want to crop the first array by using the second array x' values range 10 to 12 . I tried this;

new_array = array1[(array1>=np.amin(array2)) * (array1<= np.amax(array2))] 

It crops the first array (array1). But I can only extract x values.

[ 10.   10.1  10.2  10.3  10.4  10.5  10.6  10.7  10.8  10.9  11.   11.1
11.2  11.3  11.4  11.5  11.6  11.7  11.8  11.9  12.   12.1  12.2  12.3
12.4  12.5  12.6  12.7  12.8  12.9]

I want to extract the values of x and y from array1 by a given range of x values from another array.

Edit

array1[[  0.3     0.302   0.304   0.306   0.308   0.31    0.312   0.314   0.316
...   13.4    13.5    13.6    13.7    13.8    13.9    14.     14.1
14.2    14.3    14.4    14.5    14.6    14.7    14.8    14.9    15.   ]
[  8.82     9.       9.18     9.35     9.52     9.69     9.85    10.02
10.18    10.35    10.52    10.67    10.82    10.97    11.12    11.25
11.39    11.52 …................... 2.3044   1.7773   2.271    2.721 ]]


array2 = [[ 10.    10.02  10.03  10.04  10.05  10.06  10.07  10.08  10.09  10.1
10.12  10.13  10.14  10.15  10.16  10.17  10.18  10.19  10.2   10.21
10.22  10.23  10.24  10.25  10.26  10.27  10.28  10.29  10.3   10.31
10.33  10.34  10.35  10.36  10.37  10.38  10.39  10.4   10.41  10.42
10.43  10.44  10.45  10.46  10.47  10.48  10.49  10.5   10.51  10.52
10.53  10.54  10.59  10.64  10.7   10.75  10.8   10.85  10.9   10.95  11.
11.05  11.1   11.15 ......   12.64  12.65  12.66  12.67  12.68  12.69
12.7   12.71  12.72  12.73  12.74  12.75  12.76  12.77  12.78  12.79
12.8   12.81  12.82  12.83  12.84  12.85  12.86  12.87  12.88  12.89
12.9 ][ 0.0058  0.0073  0.0081  0.0088  0.0096  0.0104  0.0112  0.012   0.0128
0.0136  0.0165  0.0018  0.0195  0.021   0.0226  0.0241  0.0256  0.0272
0.0288  0.0334 ….  0.1092  0.0879  0.0667  0.0458  0.0433  0.0409
0.0385  0.0361  0.0337  0.0314  0.0291  0.0268  0.0245  0.0223  0.0209
0.0195  0.0182  0.0168  0.0155  0.0141  0.0128  0.0115  0.0101  0.0088
0.0085  0.0081  0.0078  0.0074  0.0071  0.0068  0.0064  0.0061  0.0058
0.0054]]

Again sorry for my English. I hope I succeded in explaining myself

Thank you a lot for your helps :)

Upvotes: 2

Views: 980

Answers (1)

user707650
user707650

Reputation:

Assuming that the first index corresponds to x, this may work:

indices = (array1[0,...] >= np.min(array2[0,...])) & (array1[0,...] <= np.max(array2[0,...]))
xselected = array1[0,indices]
yselected = array1[1,indices]

Notes: do not use np.amin, but np.min instead. Do not combine the indices together with a *, but use the boolean and: &.

I've indexed the arrays with array[0,...], but I think you can just use array[0] there as well, since the 0 indexes the first dimension.

Upvotes: 1

Related Questions