Home > Back-end >  python numpy how to insert multiple rows between each row
python numpy how to insert multiple rows between each row

Time:11-22

I have a numpy array like this:

  • 26.4812 32.0000 -5.0000 10000.0000 20000.0000 2.0000
  • 26.4812 32.0000 10.0000 10000.0000 20000.0000 2.0000
  • 26.4812 32.0000 0.0000 10000.0000 20000.0000 2.0000...

I want to change it so that the 3rd column(z value) has more steps like this:

  • 26.4812 32.0000 -5.0000 10000.0000 20000.0000 2.0000
  • 26.4812 32.0000 -4.0000 10000.0000 20000.0000 2.0000
  • 26.4812 32.0000 -3.0000 10000.0000 20000.0000 2.0000
  • ...
  • 26.4812 32.0000 9.0000 10000.0000 20000.0000 2.0000
  • 26.4812 32.0000 10.0000 10000.0000 20000.0000 2.0000...

the steps must be defined by a variable step = 1mm at this example. how can i achive that?

CodePudding user response:

This is little ugly maybe but it does what you want:

arr1 = np.array([[1, -3, -3],
           [1, 3, 3],
           [1, 3, 7]])
z=arr1[:, 2]
new_z = []
for i in range(len(z)-1):
     new_z.append(np.arange(z[i],z[i 1] 1))
new_z = np.unique(np.concatenate(new_z))
new_array = np.c_[np.repeat(arr1[0, 0], new_z.shape[0]), new_z, 
np.repeat(arr1[0, 2], new_z.shape[0])]
print(new_array)

CodePudding user response:

import numpy as np

def generate_array(zmin, zmax, step=1):
  # generate z values based on min, max and step (-5, 10, and 1 in the example)
  z_values= np.arange(zmin, zmax step, step)

  # create an array with the same data for every z value (for now)
  default_value = 0
  array = np.repeat([[26.4812, 32.0000, default_value, 10000.0000, 20000.0000, 2.0000]], len(z_values), axis=0)
  
  # replace the z values
  array[:, 2] = z_values

  return array

This can be used like this

>>> generate_array(-5, 10)
>>> array([
  [ 2.64812e 01,  3.20000e 01, -5.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01, -4.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01, -3.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01, -2.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01, -1.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  0.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  1.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  2.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  3.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  4.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  5.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  6.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  7.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  8.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  9.00000e 00,  1.00000e 04, 2.00000e 04,  2.00000e 00],
  [ 2.64812e 01,  3.20000e 01,  1.00000e 01,  1.00000e 04, 2.00000e 04,  2.00000e 00]
])
  • Related