猿问

使用 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. 我的最终结果必须是matrix5x3,这样:


1 | 5 | 3   

---------

2 | 3 | 4   

---------  

3 | 3 | 4   

---------  

4 | 2 | 5   

---------  

5 | 1 | 2  

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


我认为这个包Math.Net可以满足我的目的,但我不明白如何用它来做到这一点。


天涯尽头无女友
浏览 227回答 2
2回答

幕布斯6054654

MathNet 限制是您只能使用Double、或数字类型来实现此目的Single。ComplexComplex32using 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-Double1&nbsp; 5&nbsp; 32&nbsp; 3&nbsp; 43&nbsp; 3&nbsp; 44&nbsp; 2&nbsp; 55&nbsp; 1&nbsp; 2

慕盖茨4494581

如果我很了解你,你正在尝试做这样的事情:&nbsp; &nbsp; public static int[,] GetMatrix(IReadOnlyList<int[]> bigList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (bigList.Count == 0) throw new ArgumentException("Value cannot be an empty collection.", nameof(bigList));&nbsp; &nbsp; &nbsp; &nbsp; var matrix = new int[bigList.Count, bigList[0].Length];&nbsp; &nbsp; &nbsp; &nbsp; for (var bigListIndex = 0; bigListIndex < bigList.Count; bigListIndex++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] list = bigList[bigListIndex];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var numberIndex = 0; numberIndex < list.Length; numberIndex++) matrix[bigListIndex, numberIndex] = list[numberIndex];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return matrix;&nbsp; &nbsp; }&nbsp; &nbsp; private static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var biglist = new List<int[]>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[] {1, 2, 3, 4, 5},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[] {5, 3, 3, 2, 1},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[] {3, 4, 4, 5, 2}&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; int[,] matrix = GetMatrix(biglist);&nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < matrix.GetLength(1); i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var j = 0; j < matrix.GetLength(0); j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write($" {matrix[j, i]} ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答