Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I add periodically a new element to an array so it appears after every 500th element?

Tags:

python

numpy

If I have an array (column) that has 500k elements (numbers only), how could I be able to add a new element after every 500th element? The new number should be the average of the neighbor elements.

E.g.: between elements 499 and 500 a new element with a value of (value of 499+ value of 500)/2 and so on.

a=np.array(h5py.File('/Users/Ad/Desktop//H5 Files/3D.h5', 'r')['Zone']['TOp']['data'])
output = np.column_stack((a.flatten(order="C"))
np.savetxt('merged.csv',output,delimiter=',')

Thanks in advance!

like image 710
MONtana9999 Avatar asked Dec 04 '25 14:12

MONtana9999


2 Answers

The best way to achieve this is probably creating a generator and create a new array with that generator. Inserting into a numpy array isn't really possible, as arrays have a fixed size, so you'd need to create a new array for each insert, wich is very expensive. Using a generator you'd only need to iterate through the array once and create only one new array.

def insert_every_500th_element(np_array):
    prev = None
    for idx, value in enumarate(np_array):
        if idx != 0 and idx % 500 == 0: # we're at the 500th element - yield (499th + 500th value)/2
            yield (prev+value)/2
        yield value

a=np.array(h5py.File('/Users/Ad/Desktop//H5 Files/3D.h5', 'r')['Zone']['TOp']['data'])
a = np.fromiter(insert_every_500th_element(a), float)
output = np.column_stack((a.flatten(order="C"))
np.savetxt('merged.csv',output,delimiter=',')
like image 196
Robin Gugel Avatar answered Dec 07 '25 15:12

Robin Gugel


Here are two solutions, one in which each 500th element is edited to the new value, and another where a new value is inserted in to the array. Note that this cannot happen in-place so you must overwrite the existing array.

# if you want to edit each 500th element to a new value
iter_num = 500; i=iter_num
while i < len(array):
    array[i] = (array[i]+array[i-1])/2
    i += iter_num

# if you want a new element inserted in position 500
iter_num = 500; i=iter_num
while i < len(array):
    array = np.insert(array, i, (array[i]+array[i-1])/2)
    i += iter_num + 1
like image 35
Steinn Hauser Magnússon Avatar answered Dec 07 '25 14:12

Steinn Hauser Magnússon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!