JavaScript 自动更新数组

发布于 2024-11-28 04:00:28 字数 379 浏览 1 评论 0原文

我想做以下事情;

  1. 创建一个任意有限长度(例如 10 行)的 2 列数组,
  2. 填充后从恒定速率数据流中顺序填充它
  3. ,从同一数据流更新它(即替换元素 0,向下移动 1-9,丢弃旧的 9 )
  4. (最佳)输出每列的平均值

我自己可能可以做 4,但不知道如何做 1-3。

如果有帮助,我正在尝试翻译它;

http://kindohm.com/2011/03/01/KinectCursorControl.html (参见可怕的晃动光标下方)。

I'd like to do the following;

  1. create a 2 -column array of arbitrary finite length (say, 10 rows)
  2. populate it sequentially from a constant-rate datastream
  3. once its populated, update it from the same datastream ( ie replace element 0, move 1-9 down, discard old 9)
  4. (optimally) output an average for each column

I can probably do 4 myself, but have no idea how to do 1-3.

If it helps, I'm trying to translate this;

http://kindohm.com/2011/03/01/KinectCursorControl.html (see under the dreaded shaking cursor).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-12-05 04:00:28

这应该可以正常工作(顺便说一句,这是个好问题 - 有趣的小挑战,因为有很多方法可以做到这一点)

var pointSmoother = (function(){
  var pointCount     = 10, // number of points to keep
      componentCount = 2,  // number of components per point (i.e. 2 for x and y)
      points = [];

  function clear() {
    var i, l;
    for( i = 0, l = componentCount ; i < l ; i++ ) {
      if( typeof points[i] === 'undefined' ) {
        points.push([]);
      } else {
        points[i].splice(0, pointCount);
      }
    }
  }

  clear();

  function pushPoint( /* point components */ ) {
    var i, l;
    for( i = 0 ; i < componentCount ; i++ ) {
      points[i].unshift(arguments[i]);
      points[i] = points[i].slice(0, pointCount);
    }
  }

  function average() {
    var i, j, l, sum, averages = [];
    l = points[0].length;
    for( i = 0 ; i < componentCount ; i++ ) {
      sum = 0;
      for( j = 0 ; j < l ; j++ ) {
        sum += points[i][j];
      }
      averages.push(sum/l);
    }
    return averages;
  }

  function getPoints() {
    return points;
  }

  return {
      getPoints: getPoints,
      pushPoint: pushPoint,
      clear:     clear,
      average:   average
  };
}());

它基本上所做的是创建一个对象并将其分配给pointSmoother。该对象有 4 个方法:pushPoint()clear()getPoints()average()。在顶部,您可以设置一个点有多少个坐标,以及要保留多少个点(最大)。我用了你的例子,每个点 2 个坐标,一次 10 个点。

现在,我假设您一次获取 2 个一组的值。我们将这两个值称为 xy。当您收到它们时,使用 pointSmoother.pushPoint(x, y); 将它们添加到事物中。然后,您可以通过调用 pointSmoother.average() 来获取“平均点”,它将返回一个包含(在本例中)2 个值的数组:平均值 x 和平均值 y。

如果您想自己查看数组,可以调用 pointSmoother.getPoints() ,它将返回点数组。最后,pointSmoother.clear() 将清空先前值的数组。

这是一个演示: http://jsfiddle.net/tapqs/1/

This should work ok (nice question, by the way - fun little challenge, since there are tons of ways to do it)

var pointSmoother = (function(){
  var pointCount     = 10, // number of points to keep
      componentCount = 2,  // number of components per point (i.e. 2 for x and y)
      points = [];

  function clear() {
    var i, l;
    for( i = 0, l = componentCount ; i < l ; i++ ) {
      if( typeof points[i] === 'undefined' ) {
        points.push([]);
      } else {
        points[i].splice(0, pointCount);
      }
    }
  }

  clear();

  function pushPoint( /* point components */ ) {
    var i, l;
    for( i = 0 ; i < componentCount ; i++ ) {
      points[i].unshift(arguments[i]);
      points[i] = points[i].slice(0, pointCount);
    }
  }

  function average() {
    var i, j, l, sum, averages = [];
    l = points[0].length;
    for( i = 0 ; i < componentCount ; i++ ) {
      sum = 0;
      for( j = 0 ; j < l ; j++ ) {
        sum += points[i][j];
      }
      averages.push(sum/l);
    }
    return averages;
  }

  function getPoints() {
    return points;
  }

  return {
      getPoints: getPoints,
      pushPoint: pushPoint,
      clear:     clear,
      average:   average
  };
}());

What it basically does is it creates an object and assigns it to pointSmoother. The object has 4 methods: pushPoint(), clear(), getPoints() and average(). At the top of the thing you can set how many coordinates a point has, and how many points (maximum) to keep. I used your example of 2 coordinates per point, and 10 points at a time.

Now, I've made the assumption that you get your values in sets of 2 at a time. Let's call those 2 values x and y. When you receive them, add them to the thing by using pointSmoother.pushPoint(x, y);. You can then get the "average point", by calling pointSmoother.average() which will return an array with (in this case) 2 values: average x and average y.

If you want to look at the array yourself, you can call pointSmoother.getPoints() which will return the points array. And lastly, pointSmoother.clear() will empty the array of previous values.

Here's a demo, of sorts: http://jsfiddle.net/tapqs/1/

热情消退 2024-12-05 04:00:28
var nums = [[1,2],[1,3],[2,1]];
alert(nums[0][0]); //1 
alert(nums[0][1]); //2

那是一个二维数组。加载数据就像加载任何其他数组一样。

要按顺序更新它,您需要查看 javascript 中的队列行为。使用 unshift() 和 pop()。

Javascript 数组: http://www.w3schools.com/jsref/jsref_obj_array.asp

最后假设有 10 个位置,则对列进行平均:

var num1 = 0;
var num2 = 0;
for(int i = 0;i<10;i++)
{
    num1 +=array[i][0];
    num2 +=array[i][1];
}
num1 = num1/10;
num2 = num2/10;

//num1 is now the average of the first column
//num2 is now the average of the second column
var nums = [[1,2],[1,3],[2,1]];
alert(nums[0][0]); //1 
alert(nums[0][1]); //2

That's a two dimensional array. Loading data works just like with any other array.

To update it sequentially you are looking at queue behavior in javascript. Use unshift() and pop().

Javascript Arrays: http://www.w3schools.com/jsref/jsref_obj_array.asp

Finally for averaging the columns assuming there are 10 positions:

var num1 = 0;
var num2 = 0;
for(int i = 0;i<10;i++)
{
    num1 +=array[i][0];
    num2 +=array[i][1];
}
num1 = num1/10;
num2 = num2/10;

//num1 is now the average of the first column
//num2 is now the average of the second column
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文