0

I'm trying to check the previous 90 values in an array (or upto the start of the array) to determine if a value already exists.

So far my code just checks the previous value:

overt = np.array([])

if frame ==0: 
    overt = np.append(overt,lachange)

else: 
    pframe = frame - 1
    plachange = overt[pframe]
    if lachange ==0:
        overt = np.append(overt,lachange)
    elif lachange ==1:
        if plachange == lachange:
           overt = np.append(overt,0)
           lachange = 0
        else:
            overt = np.append(overt,lachange)

The value is 1 if the vehicle has changed lane and 0 if it hasn't but if the value is 1 multiple times in a 3 second period I want only the first one to be recorded and the following values to be 0.

2
  • 1
    Can you provide an example input array and the desired output? Commented Apr 19, 2018 at 10:11
  • you should use overt as a list instead of an array. An array is built with the specific assumption that you aren't going to be changing its size. Since you are going to be append to it on each iteration, having overt be an array actually makes the code run slower Commented Apr 19, 2018 at 10:30

1 Answer 1

1

As I understand it, what you want to do is: append 1 to overt if lachange is 1, and the last 90 values are all non-1 append 0 to over otherwise

def add_change(lachange, overt):
    if (lachange == 1) and (not (1 in overt[-90:])):
        overt.append(1)
    else
        overt.append(0)

overt[-90:] grabs the last 90 values in overt. If there are less than 90 values, it grabs everything (or nothing if overt is empty).

1 in overt[-90:] returns True if there is a 1 in it (there has been a lane change) and False otherwise. Use not to flip the values

this checks that if there is a lane change (lachange==1) and there wasn't one in the last 90 items. If so, add 1 to it. Else, add 0 to it

Code written assumes overt is a list instead of a numpy array. If it was an array, you would have to change the append statements, and return overt at the end

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.