2010-07-08 41 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中的整數數學

相關問題