/**
   * Write a CSV wordIndex to a {@link MLCell} writen to a .mat data file
   *
   * @param path
   * @throws IOException
   */
  public static void writeToMatlab(String path) throws IOException {
    Path wordMatPath = new Path(path + "/words/wordIndex.mat");
    FileSystem fs = HadoopToolsUtil.getFileSystem(wordMatPath);
    LinkedHashMap<String, IndependentPair<Long, Long>> wordIndex = readWordCountLines(path);
    MLCell wordCell = new MLCell("words", new int[] {wordIndex.size(), 2});

    System.out.println("... reading words");
    for (Entry<String, IndependentPair<Long, Long>> ent : wordIndex.entrySet()) {
      String word = ent.getKey();
      int wordCellIndex = (int) (long) ent.getValue().secondObject();
      long count = ent.getValue().firstObject();
      wordCell.set(new MLChar(null, word), wordCellIndex, 0);
      wordCell.set(new MLDouble(null, new double[][] {new double[] {count}}), wordCellIndex, 1);
    }
    ArrayList<MLArray> list = new ArrayList<MLArray>();
    list.add(wordCell);
    new MatFileWriter(fs.create(wordMatPath), list);
  }
  private void decomposePassPaths(Map<Link, Set<Path>> routes, MLCell j) {
    Network network = optimContext.network;
    Map<Id<Node>, ? extends Node> nodenetwork = network.getNodes();
    ArrayList<MLArray> paths = j.cells();
    for (MLArray path : paths) {
      MLCell cellpath = (MLCell) path;
      if (path.getM() != 0) {
        List<Node> nodelist = new ArrayList<Node>();
        List<Link> linklist = new ArrayList<Link>();
        MLDouble route = (MLDouble) (cellpath.get(0));
        double[][] nodes = route.getArray();
        for (int i = 0; i < nodes.length - 1; i++) {
          int currnode = (int) nodes[i][0];
          int nextnode = (int) nodes[i + 1][0];
          Node start = nodenetwork.get(Id.createNodeId(Integer.toString(currnode)));
          Node finish = nodenetwork.get(Id.createNodeId(Integer.toString(nextnode)));
          nodelist.add(start);
          Collection<? extends Link> possiblelinks = start.getOutLinks().values();
          for (Link l : possiblelinks) {
            if (l.getToNode().equals(finish)) {
              linklist.add(l);
              break;
            }
          }
          if (i == nodes.length - 2) {
            nodelist.add(finish);
          }

          Path temppath = convertNodeListtoPath(nodelist, linklist);
          if (routes.get(linklist.get(0)) == null) { // add path to routelist
            routes.put(linklist.get(0), new HashSet<Path>());
          }
          routes.get(linklist.get(0)).add(temppath);
        }
      }
    }
  }