在并行Colt中,如何将向量添加到矩阵的每一行,最好是就地添加?特别是,我有一个想要添加到DoubleMatrix2D的每一行的DoubleMatrix1D。这看起来应该很简单,但在Javadoc中并不清楚。(我当然可以手动完成,但似乎很奇怪没有内置这样的功能)。
发布于 2014-01-22 19:38:28
因此,要将m维向量(比如aVector)添加到nxm矩阵(比如aMatrix)的第i行,您需要执行以下操作:
// new matrix where each row is the vector you want to add, i.e., aVector
DoubleMatrix2D otherMatrix = DoubleFactory2D.sparse.make(aVector.toArray(), n);
DoubleDoubleFunction plus = new DoubleDoubleFunction() {
public double apply(double a, double b) { return a+b; }
};
aMatrix.assign(otherMatrix, plus); 关于assign方法,API是这样说的:
assign(DoubleMatrix2D y, DoubleDoubleFunction function)
Assigns the result of a function to each cell; x[row,col] = function(x[row,col],y[row,col]).我自己还没有测试过DoubleFactory2D#make()方法。如果它创建了一个矩阵,其中您的aVector在otherMatrix中合并为列而不是行,那么在使用assign()步骤之前使用DoubleAlgebra#transpose()来获得转置。
编辑
有一种简单得多的就地添加行的方法,以防您只想更改特定的(比方说第i行)行:
aMatrix.viewRow(i).assign(aVector); https://stackoverflow.com/questions/16945357
复制相似问题