write and read matrix