Monday
Jun272011
Creating a Matrix from a list of Vectors
Monday, June 27, 2011 at 12:52 Feature request 5691 was implemented in commit ca95f4f7.
We've added two new static methods in the Matrix<T> class, CreateFromRows and CreateFromColumns.
Example:
var row1 = new DenseVector(new [] { 1.0 });
var row2 = new DenseVector(new [] { 1.0, 2.0, 3.0, 4.0 });
var row3 = new DenseVector(new [] { 1.0, 2.0 });
var rowVectors = new List>{row1, row2, row3 };
var matrix = Matrix.CreateFromRows(rowVectors);
This will create a 3x4 dense matrix. The number of columns is determined by the maximum length of the given vectors (zero is used to pad short rows). Note that the storage type of the first vector determines the storage type of the matrix (i.e. a sparse vector creates a sparse matrix). CreateFromColumns works similarly.
tagged
Change Log in
Numerics
Change Log in
Numerics 



Reader Comments (1)
> let v1 = vector([1.; 2.; 3.]);;
val v1 : MathNet.Numerics.LinearAlgebra.Generic.Vector<float>
> let v2 = vector([1.1; 2.1; 3.1]);;
val v2 : MathNet.Numerics.LinearAlgebra.Generic.Vector<float>
> let v3 = vector([1.2; 2.3; 3.4]);;
val v3 : MathNet.Numerics.LinearAlgebra.Generic.Vector<float>
> let m = matrix([v1; v2; v3] |> List.map (fun e -> e |> Vector.toList));;
val m : MathNet.Numerics.LinearAlgebra.Generic.Matrix<float> =
1,2,3
1.1,2.1,3.1
1.2,2.3,3.4