Fix bug in writing matrix.