Sum multidimensional C # array

How to sort some values ​​from a multidimensional array and then calculate the average selected value?

Therefore, when I click on some image, it should display depth data (from Microsoft Kinect) not only in the place where the mouse pointer is, but it should also calculate the value in the environment (which is a multidimensional array).

This is my code:

    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;
            }
        }
    }

So, firstly: how to sum all the values ​​from d_array [m, n] ? Is it possible to calculate the sum of each row (-> one-dimensional array / vector), and then again calculate the sum of the column (-> zero-dimensional array / scalar)?

+4
source share
2 answers

, -: d_array [m, n]

:

int sum = d_array.Cast<int>().Sum();

.

(- > /), (- > /)?

, . , , :

IEnumerable<T> GetRow(T[,] array, int row)
{
    for (int i = 0; i <= array.GetUpperBound(1); ++i)
         yield return array[row, i];
}

IEnumerable<T> GetColumn(T[,] array, int column)
{
    for (int i = 0; i <= array.GetUpperBound(0); ++i)
         yield return array[i, column];
}

:

var row1Sum = GetRow(d_array, 1).Sum();
+13

IEnumerable . GetRow GetColumn " -T-". . !

  public static IEnumerable<T> GetRow<T>(T[,] array, int row)
    {
        for (int i = 0; i <= array.GetUpperBound(1); ++i)
            yield return array[row, i];
    }
    public static IEnumerable<T> GetColumn<T>(T[,] array, int column)
    {
        for (int i = 0; i <= array.GetUpperBound(0); ++i)
            yield return array[i, column];
    }
0

All Articles