2017-02-12 2 views
0

現在、データセット内のeventsの数をカウントしています(読み込んでいる.csvのデータの単一の列配列)。値が0から1に変化する回数を数えます。値が0から1に変化すると、それをイベントとして分類します。 2つのイベントの間のゼロの数が60未満の場合、同じイベントでそれらを分類します。特定の長さのデータ列のイベント数をカウントする

ここでは、20行を超えるイベントをカウントするようにコードを修正しようとしています。私はif(lines[i] != 0 && i>20)のような私の条件に入れようとしましたが、私はevの正しい値を得ていませんでした。

processData: function(data) { 
     // convert our data from the file into an array 
     var lines = data.replace(/\n+$/, "").split("\n"); 
     var ev = 0; // the event counter (initialized to 0) 
     for(var i = 0, count = 0; i < lines.length; i++) { // 'count' will be the counter of consecutive zeros 
     if(lines[i] != 0) { // if we encounter a non-zero line 
      if(count > 60) ev++; // if the count of the previous zeros is greater than 5, then increment ev (>5 mean that 5 zeros or less will be ignored) 

       count = 1; // reset the count to 1 instead of 0 because the next while loop will skip one zero 
      while(++i < lines.length && lines[i] != 0) // skip the non-zero values (unfortunetly this will skip the first (next) zero as well that's why we reset count to 1 to include this skipped 0) 
      ; 
     } 
     else // if not (if this is a zero), then increment the count of consecutive zeros 
     count++; 
     } 
     this.events = ev; 
+0

あなたはそれらに少なくとも20個のエントリを持っているイベントの数を取得したいですか? –

+0

@ibrahimmahrir正解、はい。したがって、イベントに20件未満のエントリが含まれている場合、イベントとしてカウントしたくありません。 – Gary

+0

私はあなたに最後に尋ねなかったことの1つ:イベントは値として0を持つことができますか? 60秒未満であれば、それらの0はセパレータではなくイベントの続きであることを意味しますか? –

答えて

1
processData: function(data) { 
     var lines = data.replace(/\n+$/, "").split("\n"); 
     var ev = 0; 
     for(var i = 0, count = 0; i < lines.length; i++) { 
      if(lines[i] != 0) { 
       var evcount = 1; // initialize evcount to use it to count non-zero values (the entries in an event) (1 so we won't skip the first) 
       while(++i < lines.length && lines[i] != 0) 
        evcount++; // increment every time a non-zero value is found 

       if(count > 60 && evcount > 20) ev++; // if there is more than 60 0s and there is more than 20 entries in the event then increment the event counter 

       count = 1; // initialize count 
      } 
      else // if not (if this is a zero), then increment the count of consecutive zeros 
       count++; 
     } 
     this.events = ev; 
関連する問題