【问题标题】:How to import/export Matrix from file while using UJMP library?使用 UJMP 库时如何从文件中导入/导出矩阵?
【发布时间】:2014-07-22 16:59:49
【问题描述】:

我正在尝试使用 UJMP 库。我创建了一个矩阵,我想将其导出为 txt 文件,然后将其作为基本矩阵再次导入。 我尝试了以下代码,但它不起作用:

Matrix m = MatrixFactory.dense(ValueType.INT, new long[]{(long)100,(long)100});
for(int i = 0; i< 100; i++){
        for(int j = 0; j< 100;j++){
            m.setAsInt((int) (Math.random() *3),new long[]{i,j});
        }
}
try {
        m.exportToFile(FileFormat.TXT,"test.txt",null); //this works. My file contains the matrix
} catch (MatrixException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}
Matrix n = null;
try {
        n =ImportMatrix.fromFile(FileFormat.TXT,new File("test.txt"),null); //this doesn't work. The matrix has a size of 1*1 and contains the content of the previous matrix as a single string.
} catch (MatrixException e) {
        e.printStackTrace();
} catch (IOException e) {
        e.printStackTrace();
}

System.out.println(n.getSize()[0]+" "+n.getSize()[1]); // I get 1 1;

那么我应该怎么做才能让我的原始矩阵成为 100*100 矩阵?

提前感谢您的回复,对不起我的英语。

【问题讨论】:

    标签: java matrix import export ujmp


    【解决方案1】:

    其实你可以使用方法导出矩阵到文件

    exportTo()
    

    然后选择目的地

    例如:

    m.exportTo().file(file)
    

    然后选择导出格式

    例如:

    m.exportTo().file(file).asMatlabScript("myMat");
    

    【讨论】:

    • 感谢您的回答。这大概是对的。我不确定我最终是如何绕过这个问题的,但我没有这个项目了,所以我无法测试它来验证。
    【解决方案2】:

    我不确定,但我认为您在文件中使用 null 作为分隔符:

    m.exportToFile(FileFormat.TXT,"test.txt",null);
    

    也许你应该使用:

    m.exportToFile(FileFormat.TXT,"test.txt",";");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 2011-11-24
      • 2014-08-17
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多