您应该首先将矩阵写入文件(在 Octave/Matlab 中)。这些文件具有一致的格式,允许您从中提取数据。
并且不会知道,我有一些代码可以将 Octave 矩阵解析为 .NET!别介意幕后的待办事项。这是一个更大的课程的一部分,但这应该是大部分;如果缺少任何重要的东西,请添加评论,我会挖掘它。
Dictionary<string, object> variables;
private bool ReadAllVariablesFromDataFile(string[] lines)
{
int i = 0;
while (i < lines.Length)
{
string line = lines[i];
int oldI = i;
i = ReadSingleVariable(i, lines);
}
return true;
}
private static bool IsDefinitionLine(string line)
{
return (line != null && line.TrimStart().StartsWith("#"));
}
private int ReadSingleVariable(int startIndex, string[] lines)
{
// TODO: Types other than matrix.
OctaveVariable variable = new OctaveVariable();
while (IsDefinitionLine(lines[startIndex]))
{
ProcessDefinitionLine(variable, lines[startIndex]);
startIndex++;
}
if (!variable.IsInitialized)
{
return startIndex;
}
for (int i = 0; i < variable.Rows; i++)
{
ProcessDataLine(variable, lines[startIndex + i], i);
}
this.variables.Add(variable.Name, variable.Value);
return startIndex + variable.Rows;
}
private static void ProcessDefinitionLine(OctaveVariable variable, string line)
{
string value = GetLineValue(line);
switch (GetLineId(line))
{
case "name":
variable.Name = value;
variable.IsInitialized = true;
break;
case "rows":
variable.Rows = int.Parse(value);
break;
case "columns":
variable.Columns = int.Parse(value);
break;
default:
break;
}
}
private static void ProcessDataLine(OctaveVariable variable, string line, int rowIndex)
{
string[] values = line.Trim().Split(' ');
double[] row = new double[variable.Columns];
for (int i = 0; i < variable.Columns; i++)
{
variable.Value[rowIndex, i] = double.Parse(values[i]);
}
}
private static string GetLineId(string line)
{
return line.Split(':').First().TrimStart('#').Trim().ToLowerInvariant();
}
private static string GetLineValue(string line)
{
string[] pair = line.Split(':');
if (pair.Length < 2)
{
throw new ArgumentException("Invalid def line");
}
return pair[1].Trim();
}