Rewrite function for vector and matrix.