private static void matrixToCsv(final RealMatrix matrix, final String targetFilePath) throws IOException {
        Writer out = new FileWriter(targetFilePath);

        for (int i = 0; i < matrix.getRowDimension(); ++i) {
            for (int j = 0; j < matrix.getColumnDimension(); ++j) {
                out.append("" + matrix.getEntry(i, j));
                if (j < matrix.getColumnDimension() - 1) {
                    out.append(",");
                }
            }
            out.append("\n");
            out.flush();
        }

    }