[英]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.假设我有一辆车的前视野(180 度),我想将这 180 度分成几个间隔,例如 30 度。 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)例如,如果 180 度被 45 度分割,那么这些间隔将是 (0 - 45) - 索引 0,(45 - 90) - 索引 1,(90 - 135) - 索引 2,(135 - 180) - 索引3 这意味着我将有一个大小为 4 的向量(每个间隔一个元素)
And if I have numbers for example 30 and 150 the vector would look like this: [1, 0, 0, 1]如果我有数字,例如 30 和 150,则向量将如下所示: [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但我在
state_vector[sector] = 1 list assignment index out of range
我知道肯定会有一些“愚蠢”的错字但我找不到它,再次感谢您的帮助
For equal sectors just use integer division when angle is integer:对于相等的扇区,当角度为 integer 时,只需使用 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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.