improve function to createMatrix A