【问题标题】:Parse a matrix and array of doubles to la4j object将双精度矩阵和数组解析为 la4j 对象
【发布时间】:2014-11-04 21:25:36
【问题描述】:

我已经尝试过,但找不到将整数数组和双精度矩阵解析为 la4j 矩阵/向量对象的方法。

public void fillData(int[][] data2D, int height, int width) throws IOException{
    int[] data = initializeData(height, width);
    double [][] coordinates = initializeDataCoordinates(height, width);
    Matrix a = new Basic2DMatrix();

    int index1d = 0;
    for(int row = 0; row < height; row++){
        for(int col = 0; col < width; col++){
            int y = col+1;
            int x = row+1;
            //System.out.println("Current X: " + x);
            //System.out.println("Current Y: " + y);
            double xPow = Math.pow(x, 2);
            double yPow = Math.pow(y, 2);

            coordinates[row][0] = xPow*yPow;
            coordinates[row][1] = x*yPow;
            coordinates[row][2] = yPow;
            coordinates[row][3] = xPow*y;
            coordinates[row][4] = x*y;
            coordinates[row][5] = y;
            coordinates[row][6] = xPow;
            coordinates[row][7] = x;
            coordinates[row][8] = 1;

            a.add((double)data2D[row][col]);

            data[index1d] = data2D[row][col];
            index1d++;
        }
    }
}

如您所见,我尝试使用 la4j 库的 add 方法,但矩阵保持为空。我的目标是将double[][] coordinatesint[] data的全部内容分别解析为la4j矩阵和向量。

我也尝试将这些解析为 CSV 文件,但科学记数法(由于我的 coordinates 矩阵中的数字非常大)我无法正确解析它。

有什么建议和想法吗?

【问题讨论】:

    标签: java arrays matrix


    【解决方案1】:

    事情就是这样。首先,您只需调用构造函数new Basic2DMatrix() 即可创建0x0 矩阵。其次,matrix.add 是加法运算而不是插入到矩阵中(如矩阵加其他矩阵,或在您的情况下为矩阵加值)。调用matrix.add(value) 将给定值添加到所有单元格。在您的情况下,矩阵中没有单元格 = 没有发生任何事情。您需要做的就是调用构造函数并在那里传递coordinates 数组。

    Matrix a = new Basic2DMatrix(coordinates); // easy-peasy
    

    您还可以使用get/set 等公共访问方法手动设置每个元素。

    【讨论】:

    • 感谢您的回复。我找到了解决此问题的方法,即使用 Apache Math Commons 矩阵/向量。我记得尝试使用坐标作为构造函数参数,但它不起作用,我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2011-07-07
    • 2013-03-16
    • 2015-12-30
    相关资源
    最近更新 更多