MATLAB: Mean calculation by comparison

compareMATLABmean

Hi, In the following vectors, I need to get the "mean" of y values for each set of similar x values. What I mean is I need the avg of Y for each group of x. I also need to save those mean Y values.
x=[3 3 3 4 4 5 5 5 5 3 11]; y=[1 2 4 5 7 1 1 8 10 10 1];

Best Answer

This looks like an application for accumarray:
x=[3 3 3 4 4 5 5 5 5 3 11];
y=[1 2 4 5 7 1 1 8 10 10 1];
y_means = accumarray(x', y, [], @mean);
EDIT:
To get a summary of the x-values (first column) and the corresponding y_means values (second column:
xu = unique(x);
ys_means = [xu' y_means(xu)]
produces:
ys_means =
3 4.25
4 6
5 5
11 1