Home > other >  Cut out a smaller array shape from a bigger array
Cut out a smaller array shape from a bigger array

Time:10-25

I have two sets of data in x and y arrays, one is from a theoretical calculation so it is far larger.

x1 = [1,2,3,4,5,6,7,8,9,10] y1 = [1,4,9,16,25,36,49,64,81,100]

the other is experimental so it has the same values of x (but with a smaller data set, different start and different intervals) with slightly different y values

x2 = [3,5,7,9]              y2 = [10,23,46,82] 

How can I make the 1st theoretical arrays or data have the same shapes. I want to effectively cut out 3rd/5th/7th values of the 1st arrays, making the arrays have the same shapes.

ie

x1_new = [3,5,7,9]  y1_new = [9,25,49,81]

CodePudding user response:

Python way:

y1_new = [y1[x-1] for x in x2]

Or learn numpy:

import numpy as np
y1_new = np.array(y1)[np.array(x2)-1] 
  • Related