将两个1D阵列(A []​​和B [])连接成一个2D数组(C [,])

时间:2014-09-02 02:28:32

标签: c# arrays

我有两个1D阵列:

double A = new double[7] {4, 2, 54, 16, 9, 55, 27} 

double B = new double[7] {8, 88, 21, 12, 8, 30, 11}

我如何加入那些数组成为一个2D数组C [2,6],它包含上面的1D数组?

可能是这样的:

double C = new double[2,7] {{4, 2, 54, 16, 9, 55, 27} , {8, 88, 21, 12, 8, 30, 11}};

我尝试了这段代码,但只打印了阵列A.

public static double[,] _matrix_byRow(double[] Mat1, double[] Mat2)
    {
        int i, j, y;
        double[,] newMat = new double[2, 7];

        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 7; j++)
            {
                newMat[i, j] = Mat1[j];
            }
        }
        for (i = 0; i < 2; i++)
        {
            for (y = 0; y < 7; y++)
            {
                newMat[i, y] = Mat2[y];
            }
        }
        return newMat;
    }

1 个答案:

答案 0 :(得分:1)

请改为尝试:

public static double[,] _matrix_byRow(double[] Mat1, double[] Mat2)
{
    double[,] newMat = new double[2, 7];

    for (var j = 0; j < 7; j++)
    {
        newMat[0, j] = Mat1[j];
        newMat[1, j] = Mat2[j];
    }

    return newMat;
}