下面的代码当前读取数据集,并将其输入到函数中。它将该列中的值的总和计算到 this.sum
中。现在我想扩展这段代码的功能。
我的数据集有很多 0,后跟一小段值,或“事件”。我想根据我的数据从 0 变为某个值的次数来查找这些“事件”的数量。另外,我希望将少于五个零分隔的事件放在同一个事件中。
因此,对于下面的数据集,我希望它说“发生了 2 个事件。”
我仍想保留“this.sum”,但现在希望存储 this.events
。
我习惯了 Python 和 Matlab 等脚本语言,并且对 Javascript 很陌生,因此非常感谢您的帮助!
processData: function(data) {
// convert our data from the file into an array
var lines = data.replace(/\n+$/, "").split("\n");
// we have array of strings but need an array of Numbers this should do the trick getting rid of any text and 0 values (source: http://stackoverflow.com/a/26641971)
lines = lines.map(Number).filter(Boolean);
// now we find the sum of all of the values in our array. (source: http://stackoverflow.com/a/16751601)
this.sum = lines.reduce((a, b) => a + b, 0);
this.sum = this.sum.toFixed(2);
},
数据:
0
0
0
0
0
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
0
0
0
0
0
0
0
0
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
0
0
0
0
.256
.369
.598
.145
.695
.984
.259
.368
.352
编辑:
无论我对 count
进行了哪些更改(从 5 更改为 10,再更改为 15),以下内容为我提供了错误的 ev
值(我的数据集包含的数字与上面给出的数据集。)但是当我将值更改为 500 时,它下降了,因此我将值提高到 12,000,它为我提供了我期望的 this.events
值。
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 < data.length; i++) { // 'count' will be the counter of consecutive zeros
if(lines[i] != 0) { // if we encounter a non-zero line
if(count > 5) 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;
// we have array of strings but need an array of Numbers this should do the trick getting rid of any text and 0 values (source: http://stackoverflow.com/a/26641971)
lines = lines.map(Number).filter(Boolean);
// now we find the sum of all of the values in our array. (source: http://stackoverflow.com/a/16751601)
this.sum = lines.reduce((a, b) => a + b, 0);
this.sum = this.sum.toFixed(2);
},
最佳答案
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 > 5) 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;
关于javascript - 根据不为零的变化计算事件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42126696/