c++ - 获取具有特定距离的键值对数组中的第二个最大数
问题描述
我正在做一个在脖子上绘制高度点的项目。
使用 Arduino 和霍尔传感器,我绘制了不同位置的电阻点。
- 我在 HashMap 中映射我的点(点[stepper_motor_location][resistance])
- 我需要找到中间点(环甲韧带)
- 我知道如何通过从所有位置获得最大阻力来获得上点(甲状腺软骨)。
- 但我不知道如何获得第二个最高峰但与第一个最高峰有相当距离的底部点(环状软骨)。
所以,问题是:
您如何获得点[位置] [阻力] 中的第二高阻力,与最高阻力的位置差异至少为 2-10?
C、C++、Arduino 或伪代码,我真的不介意。
这是我的代码:
/* ###### VARIABLE ######## */
/* */
found bool = false; /* while our point is not found, FOUND IS FALSE */
resistance int []; /* resistance vector */
location int []; /* Motor location vector */
current_location int = 0; /* 0 which means the top part of the motor container */
current_resistance int; /* the resistance which the Hall Sensor registers at that moment */
points HashMap; /* HashMap with location as key and resistance at its location as value */
min_distance int = 50; /* value for the minimum distante between the highest point and the second highest point (Variable to be modified as we test more) */
/* ######## FUNCTIONS ######### */
/* */
move_check()
{
location.push(current_location);
current_resistance = GET_CURRENT_RESISTANCE;
resistance.push(current_resistance);
MOVE_MOTOR++;
location++;
}
map_points()
{
for(;location <= MAX_DISTANCE;) /* WE SET THE MAX_DISTANCE TO WHAT WE MEASURED BEFORE AS MAX DISTANCE */
{
move_check();
RED_LED = 1;
RED_LED = 0; /* TO SEE THAT IS MAPPING POINTS */
}
}
CreateHashMap (points, location, resistance, MAX_DISTANCE); /* (VARIABLE_NAME , int, int, NUMBER_OF_ITEMS) */
}
get_top() /* gets the thryoid Cartilage location */
{
maximum int = 0;
max_location int = 0;
for(int i = 0; i<= location.length(); i++)
{
if(maximum > max(points[i], maximum))
{
maximum = max(points[i], maximum);
}
else
{
maximum = max(points[i], maximum);
max_location = i;
}
}
return max_location;
}
get_bottom() /* gets the cricoid cartilage location*/
{
bottom_point int;
??????????????????????????????
return bottom_point;
}
goto_middle_point()
{
RED_LED = 1;
while(location != floor((get_top() + get_bottom()) / 2))
{
location--;
}
RED_LED = 0;
BEEP_SPEAKER_LONG;
}
wait_button_press()
{
resistance_now int = GET_CURRENT_RESISTANCE;
while(resistance_now != GET_CURRENT_RESISTANCE)
{
RED_LED = 1;
RED_LED = 0;
}
SLEEP(1000);
found = true;
}
done(bool found)
{
if(found)
{
while(location)/* if location is 0 == false (top location) */
{
location--;
MOVE_MOTOR--;
}
}
BEEP_SPEAKER;
BEEP_SPEAKER; /* beep speaker twice for done */
RED_LED = 0; /* stops working */
GREEN_LED = 1; /* signal for ready */
}
int main()
{
map_points();
goto_middle_point();
wait_button_press();
done();
return 0;
}
解决方案
我不知道您正在使用哪种 Arduino HashMap 实现,但如果是这个,这是我能想到的最好的 API。它肯定不是最优的,更好的数据结构会更快。
假设我正确理解了这个问题,我会做一个线性搜索。你知道如何获得最高点。您知道第二高点的位置必须低于最高点。(我在这里添加,如果您的坐标系不同,您可能需要减去)。假设位置的最小差异必须是min_dist
(某个值 2-10)。
HashMap<int, int, N> points = ...;
int key_low = upper_position + min_dist;
int peak_location = -1;
for(int i=0; i < points.size(); ++i)
{
int location = points.keyAt(i);
if (location < key_low) continue;
int resistance = points[location];
if (peak_location < 0 || resistance > points[peak_location])
{
peak_location = location;
}
}
推荐阅读
- qlikview - 如何在循环中调用 API 并将其结果合并到 Temp 表中?
- android - 我的活动一次又一次地自动打开
- angular - 如何使用 RxJS 创建以十五分钟为增量的时间范围?
- annotations - 在mybatis dao的方法上带有自定义注解的spring aop,parameterNames为null
- html - 如何让 flexbox 填充 100% 的高度
- html - 如何让弹性项目中的项目拉伸父项的 100%?
- jupyter-notebook - 无法连接到 Google Cloud 上的 Jupyter Notebook
- javascript - 如何用花括号包裹 json 的值?
- json - 将json解组到结构时如何不允许空字段
- jquery - 在 Node.js 中抓取 HTML 列表