火炬/Lua,如何将数组配对成table?

Torch / Lua, how to pair together to arrays into a table?

我需要在我的 Torch / Lua 程序中使用 Pearson 相关系数。 这是函数:

function math.pearson(a)
  -- compute the mean
  local x1, y1 = 0, 0
  for _, v in pairs(a) do
  x1, y1 = x1 + v[1], y1 + v[2]
  print('x1 '..x1);
  print('y1 '..y1);
  end
  -- compute the coefficient
  x1, y1 = x1 / #a, y1 / #a
  local x2, y2, xy = 0, 0, 0
  for _, v in pairs(a) do
    local tx, ty = v[1] - x1, v[2] - y1
    xy, x2, y2 = xy + tx * ty, x2 + tx * tx, y2 + ty * ty
  end

  return xy / math.sqrt(x2) / math.sqrt(y2)
end

此函数需要一个可在 pairs() 函数中使用的输入对 table。 我试图向它提交正确的输入 table 但我无法得到任何工作。

我试过:

z = {}
a = {27, 29, 45, 98, 1293, 0.1}
b = {0.0001, 0.001, 0.32132, 0.0001, 0.0009}
z = {a,b}

但不幸的是它不起作用。它将计算 b 的第一个元素之间的对,而我希望它计算 a 和 b 之间的相关性。 我该怎么办?

您能否提供一个可以用作 math.pearson() 函数输入的好对象?

如果您使用的是 this implementation,那么我认为它需要不同的参数结构。您应该传递一个 table 的双值 table,如:

local z = {
  {27, 0.0001}, {29, 0.001}, {45, 0.32132}, {98, 0.0001}, {1293, 0.0009}
}
print(math.pearson(z))

这会为我打印 -0.25304101592759