Added writematrix function