将两个1D阵列(A[]和B[])合并为一个2D阵列(C[,])

本文关键字:阵列 一个 2D 合并 1D 两个 | 更新日期: 2023-09-27 18:20:12

我有两个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;
    }

将两个1D阵列(A[]和B[])合并为一个2D阵列(C[,])

试试这个:

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