created average_matrix function which creates a new matrix containing the average values for each of the specified groups