Matrix writing function added