0

Hello I need to use linspace to create points between two rows at a time in a dataset.

'location'
 1
 10
 12
 14

Essentially use linspace to find points between rows 1 & 2(1,10), then 3 & 4(12,14) (...for hundreds of rows). Any advice would be really helpful!

2 Answers2

0

If you have a list of locations, you could just iterate over the list in steps of two:

import numpy as np

# List of locations
locs = [1, 10, 12, 14] # …possibly more

# List for storing linspace output
lins = list()

# Iterate over the locations list in steps of two
for i in range(0, len(locs), 2):
    lins.append(np.linspace(locs[i], locs[i+1]))

Note that this solution only works if the number of elements in the locations list is even. If the number of elements is odd you'll need to modify the for loop; something like:

for i in range(0, len(locs)-1, 2):

Since you're asking about computing the linspace between pairs of elements, your last element would have no paired element anyway.

KsEuro
  • 328
  • 1
  • 8
0
>>> rows = [1, 10, 12, 14]
>>> n_points = 5
>>> for i in range(0, len(rows)//2*2, 2):
        print(np.linspace(*rows[i:i+2], n_points))
[ 1.    3.25  5.5   7.75 10.  ]
[12.  12.5 13.  13.5 14. ]
Woodford
  • 3,746
  • 1
  • 15
  • 29