x=[3 3 3 4 4 5 5 5 5 3 11];
y=[1 2 4 5 7 1 1 8 10 10 1];
groupBounds=[0; find(diff([x(:); NaN])~=0)];
groupAverage=arrayfun(@(gID) mean(y( (groupBounds(gID)+1):(groupBounds(gID+1)) )),1:(numel(groupBounds)-1));
fprintf('grpStartIDX grpEndIDX x_new y_new\n');
fprintf('%11d %9d %5d %5.2f\n',...
[(groupBounds(1:end-1)+1)'; ...
groupBounds(2:end)'; ...
x(groupBounds(2:end)); ...
groupAverage; ...
])
when you run it, you will get:
grpStartIDX grpEndIDX x_new y_new
1 3 3 2.33
4 5 4 6.00
6 9 5 5.00
10 10 3 10.00
11 11 11 1.00
Best Answer