fix matrix, encode directions