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;
valuecolumn = 2;
classmean = groupsummary(yourmatrix, classcolumn, 'mean', valuecolumn);
classcolumn = 1;
valuecolumn = 2;
group = findgroups(yourmatrix(:, classcolumn));
classmean = splitapply(@mean, yourmatrix(:, valuecolumn), group);
classmean = accumarray(group, yourmatrix(:, valuecolumn), [], @mean);
Best Answer