2014-09-11 83 views
1

我有1和0的輔助功能載體。 1表示天氣好; 0意味着天氣不好,而且這個地方是不可訪問的。如何在MATLAB中查找序列中的趨勢?

我有(例如)10個小時一個step_duration。考慮到step_index(步驟的開始)爲101,我需要找到一個連續10個小時天氣良好的窗口。

預期溶液:隨着10小時預期天氣,說的可接近矢量爲[0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1]。所以,我們找到窗口的索引是從109-118。和相應的天氣延遲(考慮到我們找不到直小時)是指數101-108(即9小時。)我需要編寫一個代碼,這樣的算法。

,我能想到的一些示例代碼如下(雖然這不是我想要的東西):

window = 0;  % Initialize the counter for finding the weather window. 
     for tt = step_index 
      if Accessibility(tt) == 0 
       % bad weather, move to next index 
       % reset window. 
       window = 0; 
       k = k + 1; 
       possible_window(k) = window; 
       continue 
      else 
       % Increase window 
       window = window + 1; 
       % sote window history 
       k = k + 1; 
       possible_window (k) = window; 
      end 
     end 
     tt = tt + 1; 
end 

回答

2

我不知道你後正是輸出,但你可以找到一個10人以上1氏集團很容易利用卷積:

w = [0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1] 
n = 10; 
find(conv(w, ones(1,n)) == n) - n + 1 

這給你的地方10開始的團體

+0

注意,解決方案也可以很容易地調整,以找到那些在爲10塊一個任意amound(通過改變' == n'into'> = 8')。 – 2014-09-11 12:05:49

+0

@DennisJaheruddin我不確定你的意思,這裏'conv'的結果不能大於'n',如果你想找一個更小的數字,那麼你會不會寧願設置'n = 8'? – Dan 2014-09-11 12:10:10

+0

我的意思是這個解決方案還提供了靈活性,可以在10個窗口中找到至少8個美好時刻。 - 評論主要是爲了提問者的利益。 – 2014-09-11 12:14:33

0

我認爲你正在尋找的指數具有良好的耐候,在此將被定義爲在10

舒展有足夠的人一個時期在這種情況下,你可以使用過濾器來檢查的10各舒展它可能是這樣的有多少的人出現這個:

f= filter(ones(10,1),1,x) 

不確定的語法,但檢查出來。過濾器肯定會讓你在那裏。

然後,您可以通過尋找像這樣好的時期跟進:根據strfind

find(f==10) 
find(f>=8) 
find(f==max(f)) 
1

解決發現開始的N或N +指數連續的 -

%// sample accessibility vector (different from the one in question) 
access_vec = [1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0] 

N = 10; %// N or N+ consecutive ones are to be detected 
indices = strfind([0 access_vec],[0 ones(1,N)]) 

輸出 -

indices = 
    1 13 

另一個例子 -

access_vec = [ 0 1 zeros(1,10) 1] 

輸出 -

indices = 
    [] 
+0

我喜歡這種方法。但它有效嗎?試試'a1 = [0 1 zeros(1,10)1]' – 2014-09-11 12:17:35

+0

也許你需要't1 = [1 diff(a1 == 1)0]',如果我嘗試了@Dan使用當前代碼的測試向量空。 – 2014-09-11 12:17:39

+0

您可能需要檢測'a1'中的_changes_,而不是'a1'中的'1'。我正在寫我的解決方案(現在已發佈),當我看到你的時候 – 2014-09-11 12:23:59

2

讓數據是

w = [0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1]; 
step_index = 101; 
step_duration = 10; 

然後:

d = diff([0 w 0]); 
starts = find(d==1); %// indices of starts of runs of 1's 
ends = find(d==-1); %// indices of ends of runs of 1's (actually starts of 0's) 
keep = find(ends-starts>=step_duration); %// detect runs that are long enough 
starts = starts(keep)+step_index-1; %// keep only those runs and adjust with "-1" 
ends = ends(keep)+step_index-2; %// "-2" because of how ends is defined 

結果是矢量startsends。在您的示例中,只有一個這樣的窗口,所以它們只包含一個元素:

starts = 
    109 
ends = 
    118