【发布时间】:2013-09-26 17:39:32
【问题描述】:
如何从多维数组中取出一些值,然后计算平均选择值?
所以当我单击某个图像时,它不仅应该在鼠标指针所在的点显示深度数据(来自 Microsoft Kinect),而且还应该计算环境中的值(即多维数组)。
这是我的代码:
protected void imageIR_MouseClick(object sender, System.Windows.Input.MouseEventArgs e)
{
// Get the x and y coordinates of the mouse pointer.
System.Windows.Point mousePoint = e.GetPosition(imageIR);
double xpos_IR = mousePoint.X;
double ypos_IR = mousePoint.Y;
int x = (int)xpos_IR;
int y = (int)ypos_IR;
lbCoord.Content = "x- & y- Koordinate [pixel]: " + x + " ; " + y;
int d = (ushort)pixelData[x + y * this.depthFrame.Width];
d = d >> 3;
int xpos_Content = (int)((x - 320) * 0.03501 / 2 * d/10);
int ypos_Content = (int)((240 - y) * 0.03501 / 2 * d/10);
xpos.Content = "x- Koordinate [mm]: " + xpos_Content;
ypos.Content = "y- Koordinate [mm]: " + ypos_Content;
zpos.Content = "z- Koordinate [mm]: " + (d);
// Allocating array size
int i = 10;
int[] x_array = new int[i];
int[] y_array = new int[i];
int[,] d_array = new int[i,i];
for (int m = 0; m < 10; m++)
{
for (int n = 0; n < 10; n++)
{
x_array[m] = x + m;
y_array[n] = y + n;
d_array[m, n] = (ushort)pixelData[x_array[m] + y_array[n] * this.depthFrame.Width];
d_array[m, n] = d_array[m, n] >> 3;
}
}
}
那么,首先:如何对 d_array[m,n] 中的所有值求和?是否可以计算每一行的总和(->一维数组/向量),然后再次计算列的总和(->零维数组/标量)?
【问题讨论】:
-
您可以使用本文stackoverflow.com/questions/1590723/flatten-list-in-linq 中的答案来展平多维数组。您也可以使用 linq 来计算平均值csharp.net-tutorials.org/linq-to-objects/linq-average/…。
-
你可以
SelectMany在一个锯齿状数组上,而不是一个多维数组上。
标签: c# multidimensional-array sum