向量的上半部分和下半部分的中位数
我正在尝试编译 Octave .oct 函数来计算排序向量的上半部分和下半部分的中位数,该向量的长度会有所不同,例如对于奇数长度向量,例如 [5,8,4,6,7]我想要 4,5 和 6 的“下”中值以及 6,7 和 8 的“上”中值(6 是两个计算的一部分),并且对于偶数长度向量,例如 [5,8,4 ,6,7,9]我想要 4,5 和 6 的“较低”中值以及 7,8 和 9 的“较高”中值。我也尝试使用快速方法来执行此操作并且想要使用我已经改编并用于直接中值计算的代码:-
middle = input.length()/2 + 0.5; //middle for odd-length,"upper-middle" for even length
std::nth_element(&input(0),&input(middle),&input(input.length()) );
if (input.length() % 2 != 0) { // odd length
median = input(middle);
} else { // even length
// the "lower middle" is the max of the lower half
lower_middle = *std::max_element( &input(0), &input(input.length()/2) );
median = ( input(middle) + lower_middle ) / 2.0;
}
我可以将输入向量“拆分”为理论两半
if ( input.length() % 2 != 0) { // input vector is odd length
middle = input.length()/2 + 0.5;
std::nth_element( &input(0), &input(middle), &input(input.length()) );
// *now find median of range &input(0) to &input(middle) incl.
// *and median &input(middle) to &input(input.length()) incl.
// *using fast method given above
} else { // input vector is even length
middle = input.length()/2; // uppermost value of the lower half of the input vector
std::nth_element( &input(0), &input(middle), &input(input.length()) );
// *now find median of range &input(0) to &input(middle) incl.
// *and median &input(middle + 1) to &input(input.length()) incl.
// *using fast method given above
}
我遇到的问题是我不确定应用上述*注释中值计算的语法仅指定输入向量的相关部分。我也许应该提到,输入是 Octave ColumnVector input = args(0).column_vector_value() 并且长度在 10 到 50 个值之间。
I am trying to compile an Octave .oct function to calculate the medians of the upper and lower "halves" of a sorted vector which will vary in length e.g. for an odd length vector such as [5,8,4,6,7] I want the "lower" median value of 4,5 and 6 and the "upper" median value of 6,7 and 8 (6 is part of both calculations), and for an even length vector such as [5,8,4,6,7,9] I want the "lower" median value of 4,5 and 6 and the "upper" median value of 7,8 and 9. I am also trying to use a fast method to do this and want to use this code I have adapted and use for a straight forward median calculation:-
middle = input.length()/2 + 0.5; //middle for odd-length,"upper-middle" for even length
std::nth_element(&input(0),&input(middle),&input(input.length()) );
if (input.length() % 2 != 0) { // odd length
median = input(middle);
} else { // even length
// the "lower middle" is the max of the lower half
lower_middle = *std::max_element( &input(0), &input(input.length()/2) );
median = ( input(middle) + lower_middle ) / 2.0;
}
I can "split" the input vector into theoretical halves with
if ( input.length() % 2 != 0) { // input vector is odd length
middle = input.length()/2 + 0.5;
std::nth_element( &input(0), &input(middle), &input(input.length()) );
// *now find median of range &input(0) to &input(middle) incl.
// *and median &input(middle) to &input(input.length()) incl.
// *using fast method given above
} else { // input vector is even length
middle = input.length()/2; // uppermost value of the lower half of the input vector
std::nth_element( &input(0), &input(middle), &input(input.length()) );
// *now find median of range &input(0) to &input(middle) incl.
// *and median &input(middle + 1) to &input(input.length()) incl.
// *using fast method given above
}
The problem I have is that I'm not sure of the syntax to apply the above *commented median calculations to just the indicated relevant parts of the input vector. I should perhaps mention that the input is an Octave ColumnVector input = args(0).column_vector_value() and will be between 10 to 50 values long.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 input.length() 返回 int 则应编写
middle = input.length()/2.f + 0.5;
整数值在 c 中始终使用整数数学
if input.length() returns int than you should write
middle = input.length()/2.f + 0.5;
integer values always use integer math in c