Latest Updates
Latest Tweets
Fork me on GitHub
Main | $50 Bug and Feature Bounties »
Monday
Jun272011

Creating a Matrix from a list of Vectors

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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

Unfortunately, since F# lists don't implement the IList interface, this method does not work (easily) in F#. This would work:

> 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
February 19, 2012 | Unregistered CommenterBoris

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.