added function rank_mat to re-sort rows and cols by the mean of either rows or cols