简体   繁体   中英

How to decide to which interval a number belongs

let's say that I have a car with its front field of vision (180 degrees), and I want to cut those 180 degrees into several intervals, for example by 30 degrees. and I have a set of numbers and I want to decide into which interval the number belongs to.

I know how to hard-code it but how can I automate it in order to create an algorithm which does this based on the number of intervals.

If for example the 180 degrees was split by 45 degrees than those intervals would be (0 - 45) - index 0, (45 - 90) - index 1, (90 - 135) - index 2, (135 - 180) - index 3 This means that I would have a vector of size four (one element for each interval)

And if I have numbers for example 30 and 150 the vector would look like this: [1, 0, 0, 1]

How can I achieve this?

This is what I tried to do:

NUMBER_OF_SECTORS = 6
sector_thresholds = []

for i in range(NUMBER_OF_SECTORS+1):
    sector_thresholds.append(180/NUMBER_OF_SECTORS * i)
print(sector_thresholds)

list_of_states = []
state_vector = [0] * (NUMBER_OF_SECTORS + 1)
for i in range(1000):
    random_number = 360*random.random() # If the number is larger than 180 then it should be ignored
    for j in range(NUMBER_OF_SECTORS):
        if j == 0:
            pass
        if sector_thresholds[j-1] <= random_number <= sector_thresholds[j]:
            state_vector[j] = random.choice([1,2]) #This shows an assignment error
    print(state_vector)
    state_vector = []

How can I fix this?

Thank you very much for any help

EDIT: I tried to improve my code according to the answer below, like this:

for i in range(NUMBER_OF_SECTORS+1):
    sector_thresholds.append(180/NUMBER_OF_SECTORS * i)
print(sector_thresholds)

list_of_states = []
state_vector = [0] * (NUMBER_OF_SECTORS + 1)
for i in range(1000):
    random_number = 360*random.random()
    while i % NUMBER_OF_SECTORS: # make sure that the state vector gets cleared after the same amount of iteration as its length
        sector = int(np.floor(random_number /(180/NUMBER_OF_SECTORS)))
        print(f"sector is {sector}")
        if sector <= NUMBER_OF_SECTORS:
            state_vector[sector] = 1
    print(state_vector)
    state_vector = []

but I get an error in the line state_vector[sector] = 1 list assignment index out of range I know that there will be surely some "stupid" typo but I cannot find it, thank you again for your help

For equal sectors just use integer division when angle is integer:

sector = angle // sectorsize

or flooring if angle is float

sector = int(angle / sectorsize)

Example:

import random
NUMBER_OF_SECTORS = 6
sector_size = 180 / NUMBER_OF_SECTORS

state_vector = [0] * (NUMBER_OF_SECTORS + 1)
for i in range(10):
    random_number = 360*random.random()
    sector = int(random_number / sector_size)
    print(f"sector is {sector}")
    if sector <= NUMBER_OF_SECTORS:
        state_vector[sector] = 1
print(state_vector)

>sector is 5
 ...
>[0, 0, 0, 1, 1, 1, 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM