Home > database >  Create a new array of all values from an array with step
Create a new array of all values from an array with step

Time:05-02

I am new to Python. I would like to create a new array, that contains all values from an existing array with the step.

I tried to implement it but I think there is another way to have better performance. Any try or recommendation is highly appreciated.

Ex: Currently, I have:

  1. An array: 115.200 values (2D dimension)
  2. Step: 10.000

....

array([[ 0.2735, -0.308 ],
   [ 0.287 , -0.3235],
   [ 0.2925, -0.324 ],
   [ 0.312 , -0.329 ],
   [ 0.3275, -0.345 ],
   [ 0.3305, -0.352 ],
   [ 0.332 , -0.3465],
   ...
   [ 0.3535, -0.353 ],
   [ 0.361 , -0.3445],
   [ 0.3545, -0.329 ]])

Expectation: A new array is sliced the array above by step of 10.000.

Below is my code:

for x in ecg_data:
    number_samples_by_duration_exp_temp = 10000
    # len(ecg_property.sample) = 115200
    times = len(ecg_property.sample) / number_samples_by_duration_exp_temp
    index_by_time = [int(y)*number_samples_by_duration_exp_temp for y in np.arange(1, times, 1)]
    list = []
    temp = 0
    for z in index_by_time:
        arr_samples_by_duration = ecg_property.sample[temp:z]
        list.append(arr_samples_by_duration)
        temp = z

CodePudding user response:

numpy can not be used for this purpose as len(ecg_property.sample) #115,200 is not fully divisible by number_samples_by_duration_exp_temp #10,000 and numpy cannot allow elements of varying lengths :)

You can try list comprehension.

result_list = [ecg_property.sample[temp :temp step] for temp in np.range(times)*step ]

where step=10000 and times = len(ecg_property.sample) / step

It can be further modified if needed and as per requirement.

(You can try out each step in above line of code in this answer and see the output to understand each step ) Hope this works out. ty!

  • Related