better generation of matrix