1、double[][]二维数组转成Matrix—其实根本不用这么麻烦(txt文件-double[][]—Matrix)
private Matrix GetMatrix(DataTable dt){List<double[]> datas = new List<double[]>();foreach (DataRow dr in dt.Rows){double[] a = new double[4] { (double)dr[0], (double)dr[1], (double)dr[2], (double)dr[3] };datas.Add(a);}double[][] newdata = datas.ToArray();Matrix matrix = DenseMatrix.OfColumnArrays(newdata);return matrix;}private Matrix GetMatrix(List<double[]> datas){double[][] newdata=datas.ToArray();Matrix matrix = DenseMatrix.OfColumnArrays(newdata);return matrix;}
2、排序——主要使用MathNet.Numerics.Statics的名称空间—有问题
Matrix.Column(i).OrderBy()是升序排序
Matrix.Column(i).OrderByDescending()是降序排序
private void Sort(SortMode sortmode,SortOrder sortorder){if (Src == null) return;switch(sortmode){case SortMode.None:break;case SortMode.Lon:switch (sortorder){case SortOrder.Ascend:Src.Column(1).OrderBy(a=>a);break;case SortOrder.Descend:Src.Column(1).OrderByDescending(a=>a);break;}break;case SortMode.Lat:{switch(sortorder){case SortOrder.Ascend:Src.Column(1).OrderBy(a => a);break;case SortOrder.Descend:Src.Column(1).OrderByDescending(a => a);break;}}break;case SortMode.Mileage:{switch (sortorder){case SortOrder.Ascend:Src.Column(1).OrderBy(a => a);break;case SortOrder.Descend:Src.Column(1).OrderByDescending(a => a);break;}}break;default:break;}}
3、保存至文件CSV/TXT
首先是需要引用MathNet.Numerics.Data.Matlab这个名称空间
利用Nuget安装
Install-Package MathNet.Numerics.Data.Text
Install-Package MathNet.Numerics.Data.Matlab
成功即可!
从Github或者官网上都有MathNet关于Nuget下载的说明,如下
官网:https://numerics.mathdotnet.com/
右侧Matlab能做到的栏目,点进去就可以
具体读写操作在这里
https://numerics.mathdotnet.com/CSV.html
写CSV的时候可以用Delimiter.Write
DelimitedWriter.Write(filepath, matrix, delimiter, columnHeaders);
写TXT文件时用
只需要改变文件后缀名其实就可以,并且没有columnHeaders,delimite可以用默认的(default)”\t”或者”\r”
4、读写文件得到Matrix
Delimiter.Read
//// 摘要:// Reads a MathNet.Numerics.LinearAlgebra.Matrix`1 from the given file.//// 参数:// filePath:// The path and name of the file to read the matrix from.//// sparse:// Whether the returned matrix should be constructed as sparse (true) or dense (false).// Default: false.//// delimiter:// Number delimiter between numbers of the same line. Supports Regex groups. Default:// "\s" (white space).//// hasHeaders:// Whether the first row contains column headers or not. Default: false.//// formatProvider:// The culture to use. Default: null.//// missingValue:// The value to represent missing values. Default: NaN.//// 类型参数:// T:// The data type of the Matrix. It can be either: double, float, Complex, or Complex32.//// 返回结果:// A matrix containing the data from the System.IO.TextReader.public static Matrix<T> Read<T>(string filePath, bool sparse = false, string delimiter = "\\s", bool hasHeaders = false, IFormatProvider formatProvider = null, T? missingValue = null) where T : struct, IEquatable<T>, IFormattable;
5、貌似matlab中的结构体是使用时可以定义的,然而C#的Struct不行
6、矩阵数学计算
注意操作的时候matlab的第一列的索引为1,而C#的第一列索引为0
如图为matlab代码,[E,N]是num*2维的矩阵
ones(num,1)是生成一个num维的向量。
转成C#代码
7、使用滤波程序
首先在Github上找到开源程序
https://github.com/mathnet/mathnet-filtering
我需要使用均值滤波
在其中的Src—>Median中就可以找到
然而将其引用进去也出现了OnlineFilter找不到的问题
—之后我又从Github找到的类包里把OnlineFilter.cs这个引用进来
结果发现要引用的东西实在太多了
之后我发现自己直接就错了,应该引用MathNet.Filtering这个dll
可是dll在哪里找呢
然后我在Nuget这里找到了,通过下载Nuget安装包就可以找到dll
https://www.nuget.org/packages/MathNet.Filtering
根据它提示的Package Manager语句,在VS工具-Nuget包管理器-程序包管理器控制台打开,复制粘贴入代码,就ok了
在我的项目引用中可以看到
在属性中可以找到这个dll的位置,注意他是0.7.0版本的
