I have an array of coordinates like this:
array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
I want to split the array between 6. and 7. coordinate ([5,7],[18,6]) because there is a gap in the X value there. I want to get two separate arrays, arr1 and arr2, where arr1 is the values before the split and arr2 is the values after.
I want to say that if the next X value is larger than a difference of 10, it will append to arr2, else arr1, something like this:
arr1 = []
arr2 = []
for [x,y] in array:
if next(x) > 10:
arr2.append(x,y)
else:
arr1.append(x,y)
Can someone please help me with this problem?
7 Answers 7
If I got your question correctly, you're trying to split the array at the first point where the difference between two subsequent x-values is greater than 10. You can do that using numpy:
import numpy as np
THRESH = 10
array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
array = np.asarray(array)
deltas_x = np.abs(array[1:,0] - array[:-1,0])
split_idx = np.where(deltas_x > THRESH)[0][0] + 1
arr1 = array[:split_idx,:]
arr2 = array[split_idx:,:]
Note that we need to add 1 to the result of np.where to account for the fact that the deltas_x array is 1 value shorter than array
Comments
This might be what you are looking for
array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
# Declare two array variables
arr1 = None
arr2 = None
n = len(array)
for i in range(n-1):
if abs(array[i][0] - array[i+1][0]) >= 10:
arr1 = array[:i+1]
arr2 = array[i+1:]
break
print arr1
print arr2
2 Comments
if abs(array[i][0] - array[i+1][0]) >= 10: to if abs(array[0][i] - array[0][i+1]) >= 10: just returns an IndexError: list index out of rangeout of range error since the range of for loop depends on the number of elements in array. If you want to deal with Y values, you can write an inner for loop.You can do the following:
ar = np.array([[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]])
# get differences of x values
dif = ar[1:, 0] - ar[:-1, 0]
# get the index where you first observe a jump
fi = np.where(abs(dif) > 10)[0][0]
ar1 = ar[:fi+1]
ar2 = ar[fi+1:]
Then dif would be:
array([ 1, 1, 1, 1, 0, 13, 1, -2, -7])
fi would be 5 and ar1 and ar2 would be:
array([[ 1, 6],
[ 2, 6],
[ 3, 8],
[ 4, 10],
[ 5, 6],
[ 5, 7]])
and
array([[18, 6],
[19, 5],
[17, 9],
[10, 5]]),
respectively.
That would also allow you to get all jumps in your data (you would just have to change fi = np.where(abs(dif) > 10)[0][0] to fi = np.where(abs(dif) > 10)[0])
Comments
Probably easiest to iterate over successive pairs and split as soon as a gap is found:
array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
for idx, (cur, nxt) in enumerate(zip(array, array[1:])): # successive pairs and index
if abs(cur[0] - nxt[0]) > 10: # compare difference of first items
arr1, arr2 = array[:idx+1], array[idx+1:] # split
break # no further splits, end the loop now
else: # no break, keep the original array
arr1, arr2 = array, []
Which gives:
>>> arr1
[[1, 6], [2, 6], [3, 8], [4, 10], [5, 6], [5, 7]]
>>> arr2
[[18, 6], [19, 5], [17, 9], [10, 5]]
It would be a bit more difficult if you wanted to split several times but this should well in your case.
Comments
When comparing consecutive elements I usually use enumerate:
array = [[1,6],[2,6],[3,8],[4,10],[5,6],[5,7],[18,6],[19,5],[17,9],[10,5]]
arr1 = list()
arr2 = list()
gap = 10
for index, value in enumerate(array[:-1]): # [:-1] prevents out of range
if abs(value[0]-array[index+1][0]) >= gap:
arr1.append(value)
else:
arr2.append(value)
arr2.append(array[-1]) # Take into account that the last element needs to be added to one of the arrays.
Comments
arry1 = []
arry2 = []
for i in arry:
if (i[0] - i[1]) > 10:
arry1.append(i)
else:
arry2.append(i)
2 Comments
Try this:
prev = array[0][0]
pos = -1
for i in range (1, len(array)):
if array[i][0] - prev >1:
break
else:
prev = array[i][0]
if pos != -1:
arr1 = array[:pos]
arr2 = array[pos:]
This should split array the way you want. Note that lists are indexed from 0.
> 10:is a hard-coded attempt then, the number10isn't relevant, only the split in continuity? What about[5,6],[5,7], this should be ignored too?arr1.append([x,y])to be consistent with your original list10is a hard-coded attempt, if there is a better way please let me know ;) The array size may change, but there will always be one lagre "jump" in thexvalue somewhere, and this is where I want to split the array.