convert matrix to cluster format