It's unclear how your input data is actually stored in matlab so please clarify if the below doesn't apply.
I'm assuming that you have a two column matrix with one column storing the the class and the other the column storing the values you want to average by class. In this case, this is trivially achieved with groupsummary:
classcolumn = 1; %change as appropriate
valuecolumn = 2; %can be more than one column. Each column will be average by class