在MathNet中调整矩阵大小

时间:2016-11-16 18:27:44

标签: matrix mathnet

如果我的matrixA是1x5且所有值都是1.0,并且我想通过将其他元素设置为0来将其调整为1x8,我该怎么做?

Matrix<double> A = Matrix<double>.Build.Dense(1, 5, 1.0);

换句话说,是否可以对2个不同大小的矩阵进行多次或执行任何操作?

2 个答案:

答案 0 :(得分:1)

如果你正在谈论X的1,它就是一个Vector。因此,一种选择是制作第二个向量(即1x8),然后使用

void CopySubVectorTo(Vector<T> destination, int sourceIndex, int targetIndex, int count)

将1X5的非零元素复制到较大的向量,或

void CopyTo(Vector<T> target)

答案 1 :(得分:1)

要在矩阵中执行此操作,而不是您最初描述的矢量,这里是SetSubMatrix的示例:

    Matrix<double> SubMatrix = Matrix<double>.Build.Dense(2, 2, 0.186);
    Matrix<double> BigMatrix = Matrix<double>.Build.Dense(3, 3);
    BigMatrix.SetSubMatrix(1, 1, SubMatrix);

请注意,您可以将子矩阵放在较大矩阵内的任何位置。这里是我将子矩阵放在BigMatrix(1,1)处的输出。是的,如果您知道要使用的功能,它在MathNet中可以正常工作:

enter image description here

这里它位于(0,0):

enter image description here