fix bug in read matrix function