Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 阵列的垂直平均数_Javascript_Typescript_Algorithm_Matrix - Fatal编程技术网

Javascript 阵列的垂直平均数

Javascript 阵列的垂直平均数,javascript,typescript,algorithm,matrix,Javascript,Typescript,Algorithm,Matrix,我有以下输入(3xn矩阵) 对象中的数字表示x、y、z坐标。我需要找到他们的平均分。你能帮我做一下算法吗。如何避免手动将索引设置为1,2,3。提前谢谢 我的意见如下: const length = centers.length; return [ centers.map((value) => value[0]).reduce((a, b) => a + b, 0) / length, centers.map((value) => value[1]).red

我有以下输入(3xn矩阵)

对象中的数字表示x、y、z坐标。我需要找到他们的平均分。你能帮我做一下算法吗。如何避免手动将索引设置为1,2,3。提前谢谢

我的意见如下:

 const length = centers.length;
  return [
    centers.map((value) => value[0]).reduce((a, b) => a + b, 0) / length,
    centers.map((value) => value[1]).reduce((a, b) => a + b, 0) / length,
    centers.map((value) => value[2]).reduce((a, b) => a + b, 0) / length
  ];
编辑: 有了你非常有用的评论,结果是:

centers[0].map((_, index) => centers.map(row => row[index])).map(value => value.reduce((a, b) => a + b, 0) / centers.length);

您可以在第一行使用
.map
,然后使用索引:

 centers[0].map((_, index) => 
   centers.map(row => row[index]) /*...*/
 )
 centers[0].map((_, index) => 
   centers.map(row => row[index]) /*...*/
 )