solution to generateMatrix