MATLAB: Repeated values (multiple data points at the same location).

duplicaterepeated values

Hi all,
I have an array of data and in the first column there are some repeated values (multiple data points at the same location). With those repeated values I'd like to average the associated data in column 2.
Example array: [1 3.1; 2 4.5; 3 5.7; 4 8.2; 4 5.2;]
I'd like to create a new array that looks like this: [1 3.1; 2 4.5; 3 5.7; 4 6.7;]
Thanks for any help.
-K

Best Answer

The accumarray function will do this if you ask it nicely:
ExArray = [1 3.1; 2 4.5; 3 5.7; 4 8.2; 4 5.2;];
[UExA, ia, ic] = unique(ExArray(:,1));
Out = [ExArray(ia,1) accumarray(ic, ExArray(:,2), [], @mean)]
Out =
1 3.1
2 4.5
3 5.7
4 6.7