我正在尝试开发 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 矩阵?
提前感谢您的回复,并对我的英语表示抱歉。
最佳答案
我不确定,但我认为您在文件中使用 null
作为分隔符:
m.exportToFile(FileFormat.TXT,"test.txt",null);
也许你应该使用:
m.exportToFile(FileFormat.TXT,"test.txt",";");
关于java - 使用 UJMP 库时如何从文件导入/导出矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23996859/