我觉得问这个问题很愚蠢,但我找不到一种方法来向索引或特定的索引集中添加行/向量。
我目前的解决方法是getRows(int[]),然后是addiRowVector(DoubleMatrix),然后是put(Range rs, Range cs, DoubleMatrix x):(获取行,添加向量,然后将它们放回)
这似乎是一个倒退和昂贵的实现,有没有其他选择?我错过了什么简单的东西吗?
提前感谢!
发布于 2022-04-27 00:08:12
对于一行,可以使用行号,如这段代码所示。
void oneRow() {
DoubleMatrix matrix = new DoubleMatrix(new double[][] {
{11,12,13},
{21,22,23},
{31,32,33}});
DoubleMatrix otherRow = new DoubleMatrix(new double[][] {{-11, -12, -13}});
int rowNumber = 0;
DoubleMatrix row = matrix.getRow(rowNumber);
row.addiRowVector(otherRow);
matrix.putRow(rowNumber, row);
System.out.println(matrix);
}结果你会看到
[0,000000, 0,000000, 0,000000; 21,000000, 22,000000, 23,000000; 31,000000, 32,000000, 33,000000]对于多个行,可以使用循环,例如使用行号数组。
void multipleRows() {
DoubleMatrix matrix = new DoubleMatrix(new double[][] {
{11,12,13},
{21,22,23},
{31,32,33}});
int[] rowNumbers = {0, 2};
DoubleMatrix otherRows = new DoubleMatrix(new double[][] {
{-11, -12, -13},
{-21, -22, -23}});
int otherRowsNumber = 0;
for (int r : rowNumbers) {
DoubleMatrix row = matrix.getRow(r);
row.addiRowVector(otherRows.getRow(otherRowsNumber++));
matrix.putRow(r, row);
}
System.out.println(matrix);
}为了你所看到的结果
[0,000000, 0,000000, 0,000000; 21,000000, 22,000000, 23,000000; 10,000000, 10,000000, 10,000000]https://stackoverflow.com/questions/72015005
复制相似问题