Java Jblas DoubleMatrix向量加法

Java Jblas DoubleMatrix vector addition

我觉得问这个问题真的很愚蠢,但我找不到将 row/vector 添加到索引或一组特定索引的方法。

我目前的解决方法是 getRows(int[]) 然后 addiRowVector(DoubleMatrix) 然后 put(Range rs, Range cs, DoubleMatrix x) :(获取行,添加向量,然后放回去)

这似乎是一种落后且成本高昂的实施方式,还有其他选择吗?我缺少什么简单的东西吗?

提前致谢!

对于一行,您可以像在这段代码中那样使用行号来完成

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]