Adapt the vector code to make the matrix code