forked from tsai/mathnet-numerics
4 changed files with 165 additions and 10 deletions
@ -0,0 +1,90 @@ |
|||||
|
(*** hide ***) |
||||
|
#I "../../out/lib/net40" |
||||
|
#r "MathNet.Numerics.dll" |
||||
|
#r "MathNet.Numerics.FSharp.dll" |
||||
|
|
||||
|
(** |
||||
|
MATLAB Level-5 Mat Files |
||||
|
======================== |
||||
|
|
||||
|
Level-5 MATLAB Mat files are popular as binary file container for storing one or more matrices. |
||||
|
Math.NET Numerics provides basic support for such Mat files with the **MathNet.Numerics.Data.Matlab** package, |
||||
|
which is available on NuGet as separate package and not included in the basic distribution. |
||||
|
|
||||
|
|
||||
|
Reading matrices from a MATLAB file |
||||
|
----------------------------------- |
||||
|
|
||||
|
The `MatlabReader` class provides static functions to list all matrices stored in a MAT file or stream, |
||||
|
and to read them individually as Math.NET matrices: |
||||
|
|
||||
|
[lang=csharp] |
||||
|
using MathNet.Numerics.LinearAlgebra; |
||||
|
using MathNet.Numerics.Data.Matlab; |
||||
|
|
||||
|
// read the first matrix as double |
||||
|
Matrix<double> m = MatlabReader.Read<double>("collection.mat"); |
||||
|
|
||||
|
// read a specific matrix named "vd": |
||||
|
Matrix<double> m = MatlabReader.Read<double>("collection.mat", "vd"); |
||||
|
|
||||
|
// we can also choose to convert to a different type: |
||||
|
Matrix<Complex> m = MatlabReader.Read<Complex>("collection.mat"); |
||||
|
|
||||
|
// read all matrices of a file by name into a dictionary |
||||
|
Dictionary<string,Matrix<double>> ms = |
||||
|
MatlabReader.ReadAll<double>("collection.mat"); |
||||
|
|
||||
|
// read the matrices named "Ad" and "vd" into a dictionary |
||||
|
var ms = MatlabReader.ReadAll<double>("collection.mat", "vd", "Ad"); |
||||
|
|
||||
|
Alternatively the reader can list all matrices of a file into named data elements, |
||||
|
which can then be read into matrices individually. This is useful e.g. if we need to |
||||
|
read some of the matrices to a different type: |
||||
|
|
||||
|
[lang=csharp] |
||||
|
List<MatlabMatrix> ms = MatlabReader.List("collection.mat"); |
||||
|
Matrix<double> Ad = MatlabReader.Unpack<double>(ms.Find(m => m.Name == "Ad")); |
||||
|
Matrix<float> vd = MatlabReader.Unpack<float>(ms.Find(m => m.Name == "vd")); |
||||
|
|
||||
|
|
||||
|
Writing matrices to a MATLAB file |
||||
|
--------------------------------- |
||||
|
|
||||
|
The dual to the reader above is the `MatlabWriter` class that can serialize matrices |
||||
|
to a MATLAB file or stream. Like the reader, the writer can use `MatlabMatrix` data elements |
||||
|
to compose packed matrices into a file. Each matrix has a name which must not contain spaces. |
||||
|
|
||||
|
[lang=csharp] |
||||
|
var matrices = new List<MatlabMatrix>(); |
||||
|
m.Add(MatlabWriter.Pack(myFirstMatrix, "m1"); |
||||
|
m.Add(MatlabWriter.Pack(mySecondMatrix, "m2"); |
||||
|
MatlabWrier.Store("file.mat", matrices); |
||||
|
|
||||
|
But there are also direct routines if only a single matrix or matrices of all the same data type |
||||
|
are to be stored in a file: |
||||
|
|
||||
|
[lang=csharp] |
||||
|
// write a single matrix "myMatrix" and name it "m1". |
||||
|
MatlabWriter.Write("file.mat", myMatrix, "m1"); |
||||
|
|
||||
|
// write multiple matrices, from a list of matrices and a list of their names: |
||||
|
MatlabWriter.Write("file.mat", new[] { m1, m2 }, new[] { "m1", "m2" }); |
||||
|
|
||||
|
// write a dictionary of matrices: |
||||
|
var dict = new Dictionary<string, Matrix<double>>(); |
||||
|
dict.Add("m1", m1); |
||||
|
dict.Add("m2", m2); |
||||
|
MatlabWriter.Write("file.mat", dict); |
||||
|
|
||||
|
|
||||
|
Alternatives |
||||
|
------------ |
||||
|
|
||||
|
The data extension packages also offer other ways to serialize a matrix to a text file. |
||||
|
Among others: |
||||
|
|
||||
|
* [Delimited Text Files (CSV & TSV)](CSV.html) |
||||
|
* [NIST MatrixMarket text files](MatrixMarket.html) |
||||
|
|
||||
|
*) |
||||
@ -0,0 +1,62 @@ |
|||||
|
(*** hide ***) |
||||
|
#I "../../out/lib/net40" |
||||
|
#r "MathNet.Numerics.dll" |
||||
|
#r "MathNet.Numerics.FSharp.dll" |
||||
|
|
||||
|
(** |
||||
|
NIST MatrixMarket Text Files |
||||
|
============================ |
||||
|
|
||||
|
MatrixMarket is both a [vast repository of test data](http://math.nist.gov/MatrixMarket/) |
||||
|
and a text-based [exchange file format](http://math.nist.gov/MatrixMarket/formats.html) provided by NIST. |
||||
|
Being text-based makes it convenient to deal with and program against, and also works well with versioning |
||||
|
tools like [Git](http://www.git-scm.com/). But other than [CSV](CSV.html) it can also store sparse matrices in a compact way. |
||||
|
|
||||
|
Math.NET Numerics provides basic support for MatrixMarket files with the **MathNet.Numerics.Data.Text** package, |
||||
|
which is available on NuGet as separate package and not included in the basic distribution. |
||||
|
|
||||
|
|
||||
|
Reading a matrix from a MatrixMarket file |
||||
|
----------------------------------------- |
||||
|
|
||||
|
The `MatrixMarketReader` class provides static functions to read a matrix or a vector from a file or string. |
||||
|
It can read from: |
||||
|
|
||||
|
* **TextReader**: If you have your delimited data already in memory in a string, |
||||
|
you can use this method using a StringReader. |
||||
|
* **Stream**: read directly from a stream, e.g. a MemoryStream, FileStream or NetworkStream. |
||||
|
* **File Path (string)**: read from a file, specified by the file system path. Optionally GZip compressed. |
||||
|
|
||||
|
All these functions expect the data type of the matrix to be generated as generic type argument. |
||||
|
Only Double, Single, Complex and Complex32 are supported. |
||||
|
|
||||
|
Example: |
||||
|
|
||||
|
[lang=csharp] |
||||
|
using MathNet.Numerics.Data.Text; |
||||
|
|
||||
|
Matrix<double> matrix = MatrixMarketReader.ReadMatrix<double>("fidap007.mtx"); |
||||
|
|
||||
|
|
||||
|
Writing a matrix to a MatrixMarket file |
||||
|
--------------------------------------- |
||||
|
|
||||
|
The dual to the reader above is the `MatrixMarketWriter` class that can serialize a matrix or vector |
||||
|
to a MatrixMarket text file, stream or TextWriter. |
||||
|
|
||||
|
Example: |
||||
|
|
||||
|
[lang=csharp] |
||||
|
MatrixMarketWriter.WriteMatrix("matrix.mtx", m); |
||||
|
|
||||
|
|
||||
|
Alternatives |
||||
|
------------ |
||||
|
|
||||
|
The data extension packages also offer other ways to serialize a matrix to a binary stream or file. |
||||
|
Among others: |
||||
|
|
||||
|
* [Delimited Text Files (CSV & TSV)](CSV.html) |
||||
|
* [MATLAB Level-5 Mat files](MatlabFiles.html) |
||||
|
|
||||
|
*) |
||||
Loading…
Reference in new issue