The short story is: I am trying to delete a precise point in an array, by using a logical zero or any other way.
I am starting with some points' coordinates which define a wing. With them, I am trying to form a nice polygon with no crossing lines. This is done by starting at point one (any one point), and then filling in the next point as the point closest to the first point.
The coordinates are loaded as xEdge and yEdge. First thing I do is create a copy of them with only 0 in it as follow. xEdgeOrdered and yEdgeOrdered will be filled with the polygon points. I will also create one with only ones which will be used to check which values are already taken by the polygon.
xEdgeOrdered = np.zeros_like(xEdge)
yEdgeOrdered = np.zeros_like(xEdge)
notUsed = np.ones_like(xEdge)
We then start our polygon with the y-maximum for instance, and here starts the problem.
startIndex = np.argmax(yEdge)
xEdgeOrdered[0] = xEdge[startIndex]
notUsed[startIndex] = 0
In the last line, when I state notUsed[startIndex] = 0, what I'm really trying to do is replace the "USED" value as a logical 0 -- the "false" in matlab. The aim is that this value is not reachable any more, basically deleted from the array.
I have tried to mask it (see below), but I find it doesn't work perfectly in the script's next step.
notUsed = np.ma.masked_where(notUsed == 0, notUsed)
The next step is a loop which finds the next closest points. I will first try to describe what I am trying to do in words and I will attach my code after (which does not work yet...).
Starting from the initial point 1, I need to find the next closest point using vector length. I will try all remaining points. To know which are the remaining points, I give my function the argument "notUsed", which remember is an array with "1" if the point is not used, or 0 if it is used (by 0 we would like to mean "false" but we haven't found how yet). What I have done is make a carbon copy of xEdge as xEdgewhile, set the value we just used to 0, then ask that values = 0 should be masked
Once the minimum value is found, the index position is recorded. Using it, we can fill this new point in xEdgeOrderedn and continue the loop starting from this new point. However, before that, we need to delete the index we just used so that it is not accessible anymore. If we were in matlab, we would do notUsed(index) = false; - the question is How can I do this in Python?
This is the code I came up with:
i, z, min = 1, 0, 'inf'
xEdgewhile = xEdge + []; yEdgewhile = yEdge + [];
while i < len(xEdge):
i = i + 1
notUsedIndices = indexVector #This line might be useless
while z < len(xEdge):
distance = math.sqrt((xEdgewhile[z] - xEdgeOrdered[(i-1)])**2 + (yEdgewhile[z] - yEdgeOrdered[(i - 1)])**2 )
if distance < min:
min, distance_min, = distance, z
z = z + 1
print min
xEdgeOrdered[i] = xEdge[distance_min]
yEdgeOrdered[i] = yEdge[distance_min]
xEdgewhile[distance_max], yEdgewhile[distance_min] = 0, 0
xEdgewhile = np.ma.masked_where(xEdgewhile == 0, xEdgewhile)
yEdgewhile = np.ma.masked_where(yEdgewhile == 0, yEdgewhile)
For reference, I can also give you the matlab code if it helps. Long story short, I am trying to delete a precise point in an array, by using a logical zero or any other way.