How to take one row of two dimension array?

I want to have a bicubic interpolation by using C# , but I can't deal with array.

 double cubicInterpolate(double[] p , double x)
    {
        return p[1] + 0.5 * x * (p[2] - p[0] + x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] + x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
    }

    double bicubicInterpolate(double[,] p , double x, double y)
    {
        double [] arr = new double[4];
        arr[0] = cubicInterpolate(p[][0], y);
        arr[1] = cubicInterpolate(p[][1], y);
        arr[2] = cubicInterpolate(p[][2], y);
        arr[3] = cubicInterpolate(p[][3], y);
        return cubicInterpolate(arr, x);
    }
Jon Skeet
people
quotationmark

You can't, basically. You've got a rectangular array, which is a single block of memory. You could write your own wrapper around that, e.g.

public RectangularArrayRow<T> : IList<T>
{
    private readonly int row;
    private readonly T[,] array;

    public RectangularArrayRow(T[,] array, int row)
    {
        // TODO: Validation
        this.row = row;
        this.array = array;
    }

    public T this[int index]
    {
        get { return array[row, index]; }
        set { array[row, index] = value; }
    }

    // etc
}

But if you want to be able to just get a "subarray" directly, you'll need to use a jagged array to start with:

double[][] array = new double[10][];
for (int i = 0; i < array.Length; i++)
{
     array[i] = new double[3];
}
// ...
double[] row = array[0]; // or whatever

So your bicubicInterpolate method would become:

// Note: name changed to to be conventional
double BicubicInterpolate(double[][] p, double x, double y)
{
    double[] arr = new double[4];
    arr[0] = CubicInterpolate(p[0], y);
    arr[1] = CubicInterpolate(p[1], y);
    arr[2] = CubicInterpolate(p[2], y);
    arr[3] = CubicInterpolate(p[3], y);
    return CubicInterpolate(arr, x);
}

people

See more on this question at Stackoverflow