【问题标题】:How to fill a three dimensional array如何填充一个三维数组
【发布时间】:2014-01-27 15:42:33
【问题描述】:

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

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

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

这个我试过了,还是不行

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

【问题讨论】:

  • 如果您将其设为double[,][] matrix;,您可以使用单个分配,但这是一个浅拷贝。不同的尺寸似乎也需要这个。
  • @HenkHolterman 我很困惑哪个和哪个,[groupSize][columnSize, valueSize] 这样?
  • [组、列][valueIndex]
  • 伙计们,请有人给我一个正确的答案,我仍然在matrix = new double[groupSize, columnSize][valueSize]; 上遇到错误:Invalid rank specifier: expected ',' or ']'
  • 它是double[groupSize, columnSize][] 或者double[][groupSize, columnSize]。您无法一次性设置。

标签: c# arrays matrix multidimensional-array


【解决方案1】:

根据@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();
    }
}

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多