c# - 如何填充三维数组

标签 c# arrays matrix multidimensional-array

我想用以下数组填充一个三维数组:

double[] y11 = new double[7] { 24, 13.3, 12.2, 14, 22.2, 16.1, 27.9 };
double[] y12 = new double[7] { 3.5, 3.5, 4, 4, 3.6, 4.3, 5.2 };

double[] y21 = new double[7] { 7.4, 13.2, 8.5, 10.1, 9.3, 8.5, 4.3 };
double[] y22 = new double[7] { 3.5, 3, 3, 3, 2, 2.5, 1.5 };

double[] y31 = new double[5] { 16.4, 24, 53, 32.7, 42.8 };
double[] y32 = new double[5] { 3.2, 2.5, 1.5, 2.6, 2 };

double[] y41 = new double[2] { 25.1, 5.9 };
double[] y42 = new double[2] { 2.7, 2.3 };

例如y12表示第1组中的数组,第2列,依此类推。所以我有 4 组,每组有 2 列。

public class Matrix
{
    double[, ,] matrix;
    public void Initial(int groupSize, int columnSize, int valueSize)
    {
        matrix = new double[groupSize, columnSize, valueSize];
    }
}

我需要一个简单灵活的矩阵 Add 方法,而不是像 matrix[1][2][3] = value; 那样分配每个值;

我已经尝试过了,但没能成功

public void Add(double[] columnArray, int groupIndex, int columnIndex)
{
    matrix[i, y] = columnArray;
}

最佳答案

根据@Henk Holterman的评论(谢谢),我已经成功解决了这个问题

public class Matrix
{
    double[,][] matrix;
    public Matrix(int groupSize, int columnSize)
    {
        matrix = new double[groupSize, columnSize][];
    }
    public void Add(double[] arr, int groupIndex, int columnIndex)
    {
        matrix[groupIndex, columnIndex] = arr;
    }
    public void Print()
    {
        int columnIndex = 0;
        int groupIndex = 0;
        int groupSize = matrix.GetLength(0);
        int columnSize = matrix.GetLength(1);
        while (groupIndex < groupSize)
        {
            for (int k = 0; k < matrix[groupIndex, columnIndex].Length; k++)
            {
                Console.Write(groupIndex + 1);
                while (columnIndex < columnSize)
                {
                    Console.Write("        {0}", matrix[groupIndex, columnIndex][k]);
                    columnIndex++;
                }
                Console.WriteLine();
                columnIndex = 0;
            }
            groupIndex++;
        }
    }

}

主类

    static Matrix m;
    static void SetDataSet()
    {
        double[] y11 = new double[7] { 24, 13.3, 12.2, 14, 22.2, 16.1, 27.9 };
        double[] y12 = new double[7] { 3.5, 3.5, 4, 4, 3.6, 4.3, 5.2 };

        double[] y21 = new double[7] { 7.4, 13.2, 8.5, 10.1, 9.3, 8.5, 4.3 };
        double[] y22 = new double[7] { 3.5, 3, 3, 3, 2, 2.5, 1.5 };

        double[] y31 = new double[5] { 16.4, 24, 53, 32.7, 42.8 };
        double[] y32 = new double[5] { 3.2, 2.5, 1.5, 2.6, 2 };

        double[] y41 = new double[2] { 25.1, 5.9 };
        double[] y42 = new double[2] { 2.7, 2.3 };

        m.Add(y11, 0, 0);
        m.Add(y12, 0, 1);
        m.Add(y21, 1, 0);
        m.Add(y22, 1, 1);
        m.Add(y31, 2, 0);
        m.Add(y32, 2, 1);
        m.Add(y41, 3, 0);
        m.Add(y42, 3, 1);
    }
    static void Main(string[] args)
    {
        m = new Matrix(4,2);
        SetDataSet();
        m.Print();
        Console.ReadLine();
    }
}

enter image description here

关于c# - 如何填充三维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21014303/

相关文章:

c++ - 是否有一种直接的方法来反转三角形(上或下)矩阵?

c# - 如何从 RichTextBox 中获取显示的文本?

c# - 如何在 C# 打印中设置打印点

java - 随机值生成

python - Numpy:尝试在数组切片的切片上设置值

java - 调用/调用 void 方法(Java 作业 - 生命游戏示例)

c# - 使用 XMLSerializer 反序列化 XML

c# - 向内容类型添加字段时为什么要使用 fieldLinks?

php - foreach 数组是更改网页上 sql 列名称的最佳选择吗?

javascript - 重新校准使用 css 变换的 x 和 y 偏移