2010-07-08 23 views
1

我想编译一个八度.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输入= args(0).column_vector_value(),并且长度将在10到50个值之间。

回答

0

如果input.length()返回值INT比应该写

中间= input.length()/ 2.F + 0.5;

整数值总是使用c中的整数数学

相关问题