我有一些大的带标题的 CSV 文件,其中包含我想加载到矩阵中的值。
我遇到了 LineSplittingParser 类,但找不到关于如何使用它从文件中实际获取 Matrix 对象的好例子。
谁能举一个小例子来说明如何实现这一目标?
不确定 LineSplittingParser 是读取矩阵的好选择,但这样的事情可能会起作用:
File fileToParse = null;
// You have to know the number of rows and columns
int numRows = 9, numCols = 9;
Primitive64Store matrix = Primitive64Store.FACTORY.make(numRows, numCols);
LineSplittingParser parser = new LineSplittingParser("\\s+", true);
AtomicInteger row = new AtomicInteger();
boolean skipHeader = true;
parser.parse(fileToParse, skipHeader, line -> {
int i = row.intValue();
for (int j = 0; j < line.length; j++) {
matrix.set(i, j, Double.parseDouble(line[j]));
}
row.incrementAndGet();
});
这段代码未经测试——它只是一个大纲——你必须"\\s+"
用文件中使用的任何分隔符替换。