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

Time:10-13

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!

CodePudding user response:

You can use numpy.insert, though keep in mind that it returns a new array because arrays in NumPy have fixed size.

>>> import numpy as np
>>> a = np.asarray([1,2,3,4])
>>> np.insert(a, 2, 66)
array([ 1,  2, 66,  3,  4])

CodePudding user response:

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=',')

CodePudding user response:

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
  • Related