2014-02-13 72 views
0

我有一個列向量,我試圖編寫一個函數來實現一個變量窗口函數。 這意味着我想選擇一行並跳過許多行(這是可變部分),但不僅是跳過,我還要設置被跳過的行中某一列的值等於所選行之前他們在同一列。列是:選擇矩陣矩陣的特定行(開窗函數)

---------- 
    P1 
---------- 
    P2 
---------- 
    P3 
---------- 
    P4 
---------- 

因此我們的目標是創建P1 P1 P3 P3 P4 P4新列...可變部分是指在功能改變一個變量,它可以創建一個新列與P1 P1 P1 P4 P4 P4 P7 P7 P7 ...

我厭倦了這樣的事:(實行第一種情況)

% column vector containing P values a 
    a ; 

    delay = 0; 
    % f parameter to enter the delay processing 
    f = 2; 

    r = length(a); 
    i = 1; 
    while(i <= r) 
    if(mod(i, f) == 0) 
     for j = 0 : delay 
      a(i + j) = a(i - 1); 
     end 
     i = i + delay + 1; 

    else 
     i = i + 1; 
    end 
    end 

我認爲這個問題是在使用MOD功能或選擇f的值。

任何幫助表示讚賞。

+1

請出示你的努力。 –

回答

0

回答如下,包括窗口之間的比較和保存所有結果在一個數組:

a = v; 
r = length(a); 
i = 1; 
all_Results = []; 
Vectors = []; 
for window =1:128 ; 

while(i < r) 

     for w = 1 : window; 
      if (i < r) 
      i = i+1; 
      a(i) = a(i-1); 
      end 
     end 
     i = i + 1 ;  
end 
equal_elements = length(find(a==t)); 
accuracy = equal_elements/ length(t); 
Results = [ window , accuracy ]; 
Vectors = [Vectors , a ]; 
all_Results = [all_Results; Results]; 
a = v; 
i = 1; 
end 
0

這裏我的建議。我覺得有點簡單。 爲了使索引向量形狀正確,我借用瞭解決方案A similar function to R's rep in Matlab。這與你的問題非常相似。

%# random vector, parameter i to enter the delay processing 
vector = rand(1000000,1); 
i=2; 

%# entries of vector to be duplicated 
repeat = 1:i:length(vector); 

%# fill gaps in this index vector and cut to same length 
matrix = repmat(repeat,i,1); 
index = matrix(:); 
index(length(vector)+1:end) = []; 

%# generate desired result 
vector = vector(index); 

時序我的機器上這些參數:Elapsed time is 0.055114 seconds.