繁体   English   中英

使用Math.Net从数组创建矩阵

[英]Create matrix from array with Math.Net

我有一个由数字子列表组成的列表。 这被称为大biglist ,它是:

biglist[0] = { 1, 2, 3, 4, 5 };
biglist[1] = { 5, 3, 3, 2, 1 };
biglist[2] = { 3, 4, 4, 5, 2 };

现在,我想使用这些子列表创建一个matrix ,其中每个子列表代表matrix 我的最终结果必须是这样的matrix 5x3:

1 | 5 | 3   
---------
2 | 3 | 4   
---------  
3 | 3 | 4   
---------  
4 | 2 | 5   
---------  
5 | 1 | 2  

我知道如何将list转换为array但不知道如何组装这些数组以创建matrix

我认为Math.Net软件包Math.Net我的需求,但是我不知道如何使用它来实现。

MathNet 限制是,您只能MathNet使用DoubleSingleComplexComplex32数字类型。

using MathNet.Numerics.LinearAlgebra;

// ...

double[][] biglist = new double[3][];

biglist[0] = new double[] { 1, 2, 3, 4, 5 };
biglist[1] = new double[] { 5, 3, 3, 2, 1 };
biglist[2] = new double[] { 3, 4, 4, 5, 2 };

Matrix<double> matrix = Matrix<double>.Build.DenseOfColumns(biglist);
Console.WriteLine(matrix);

得到:

DenseMatrix 5x3-Double
1  5  3
2  3  4
3  3  4
4  2  5
5  1  2

如果我非常了解您,您正在尝试执行以下操作:

    public static int[,] GetMatrix(IReadOnlyList<int[]> bigList)
    {
        if (bigList.Count == 0) throw new ArgumentException("Value cannot be an empty collection.", nameof(bigList));

        var matrix = new int[bigList.Count, bigList[0].Length];

        for (var bigListIndex = 0; bigListIndex < bigList.Count; bigListIndex++)
        {
            int[] list = bigList[bigListIndex];

            for (var numberIndex = 0; numberIndex < list.Length; numberIndex++) matrix[bigListIndex, numberIndex] = list[numberIndex];
        }

        return matrix;
    }

    private static void Main(string[] args)
    {
        var biglist = new List<int[]>
        {
            new[] {1, 2, 3, 4, 5},
            new[] {5, 3, 3, 2, 1},
            new[] {3, 4, 4, 5, 2}
        };

        int[,] matrix = GetMatrix(biglist);

        for (var i = 0; i < matrix.GetLength(1); i++)
        {
            for (var j = 0; j < matrix.GetLength(0); j++)
                Console.Write($" {matrix[j, i]} ");
            Console.WriteLine();
        }


        Console.ReadKey();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM