【问题标题】:How to convert an integer array to a matrix in c#如何在c#中将整数数组转换为矩阵
【发布时间】:2015-05-12 04:49:04
【问题描述】:

我有一个名为resp 的整数数组,我想将其重写/转换为row matrix,名称为resp

int[] resp= {1, 0, 1, 0};

我正在使用Mathnet.Numerics 库。

我该怎么做?

【问题讨论】:

  • 数组resp的定义是什么?你实际上没有提供任何人需要给你一个适用的答案的信息。
  • 这已经看起来像[1, 0, 1, 0] 的行矩阵(行向量?)。你的预期输出是什么?
  • 输出将与 [1, 0, 1, 0] 相同;只有对象类型应该改变。
  • 好的,那么想要转换成什么类型​​呢?据我所知,.NET 没有表示行向量的特定类型,但是您现在拥有的数组是一个合理的近似值。您是否使用了未在问题中包含相关信息的库?
  • 我已经编辑了任何人为您的问题提供合理答案所需的关键最低信息。将来,请尽力在您的问题的第一次迭代中包含此信息。谢谢!

标签: c# mathnet-numerics


【解决方案1】:

在 Mathnet 中,您无法初始化整数数组。事实上,对此提供的支持有限。如果你尝试过,你会得到这个:

Unhandled Exception: System.TypeInitializationException: The type initializer 
for 'MathNet.Numerics.LinearAlgebra.Vector`1' threw an exception. ---> 
System.NotSupportedException: Matrices and vectors of type 'Int32' 
are not supported. Only Double, Single, Complex or Complex32 are supported at this point.

您可以像这样初始化具有相似值(使用双精度值)的向量:

var resp = new [] {1.0, 0.0, 1.0, 0.0};
var V = Vector<double>.Build;
var rowVector = V.DenseOfArray(resp);

为了构建矩阵,您需要一个多维数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多