gpt4 book ai didi

java - 在 Parallel Colt 中添加矩阵和 vector

转载 作者:行者123 更新时间:2023-11-30 04:21:28 24 4
gpt4 key购买 nike

在 Parallel Colt 中,如何将 vector 添加到矩阵的每一行,最好是就地添加?特别是,我有一个 DoubleMatrix1D,我想添加到 DoubleMatrix2D 的每一行。看起来这应该很简单,但 Javadoc 中并不清楚。 (我当然可以手动完成,但没有内置这样的功能似乎很奇怪)。

最佳答案

因此,要将 m 维 vector (例如 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);

API 对 assign 方法是这么说的:

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 中的行合并,则使用 DoubleAlgebra#transpose() 获取转置在使用 assign() 步骤之前。

编辑

如果您只想更改特定(例如第 i)行,则有一种更简单的就地添加行的方法:

aMatrix.viewRow(i).assign(aVector); 

关于java - 在 Parallel Colt 中添加矩阵和 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16945357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com