public static void printMatrix(double[][] matrix, String fp, String[] names, int[] perm, String format) throws FileNotFoundException {
        File f = new File(fp);
        FileUtils.makeSubDirsOnly(f);
        PrintWriter out = new PrintWriter(f);

        if (names != null) {
            out.print("#");
            for (int i = 0; i < names.length; i++) {
                out.print(SEPARATOR);
                out.print(names[(perm == null) ? i : perm[i]]);
            }
            out.println();
        }

        for (int i = 0; i < matrix.length; i++) {
            if (names != null) {
                out.print(names[(perm == null) ? i : perm[i]] + SEPARATOR);
            }
            for (int j = 0; j < matrix[i].length; j++) {
                if (j > 0) {
                    out.print(SEPARATOR);
                }
                if (perm == null) {
                    out.printf(format, matrix[i][j]);
                } else {
                    out.printf(format, matrix[perm[i]][perm[j]]);
                }
            }
            out.println();
        }

        out.close();
    }