[英]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
使用Double
, Single
, Complex
或Complex32
數字類型。
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.