MATLAB: Finding neighbour values average of the 2d data set

statistics

Hi,
I have a 2D data sets where x is distance and y is velocity. I want to take average of the y neighbour values based on on the certain distance of the x.
x= [0 0.6864 2.6457 5.6277 5.8258 7.0811 7.3331 15.2125 15.7822 15.9928 22.3321 25.0864 25.1076 26.9789 34.8243 34.7256 35.8708 35.7982 39.3342 39.8397 39.9886 44.9403 47.7513 51.9495 52.3910 56.4902 56.5600 59.4989 61.0052 64.1030 66.1371 68.0970 67.9998 68.5739 71.9889 73.3882 73.3983 73.5973 74.3927 75.2329 75.3533 83.6463 92.9018]
&
y=[18.0764 10.9711 14.8436 10.2461 6.0926 10.6717 12.0432 7.1698 6.4452 4.3970 0.8860 2.2658 6.3352 3.2598 9.9865 9.9774 9.8610 9.8632 1.7850 8.3102 7.5321 2.8751 8.4332 0.1547 3.2476 9.1322 9.7377 0.0133 7.9703 9.7445 4.9609 3.8988 2.6878 5.7202 7.5607 7.4299 5.9566 9.1900 7.6760 14.3481 12.9469 30.5523 17.8944]
Note: for 0-1 value difference between two consecutive x values I want to take average of the particular y values (for x 5.6277 and 5.8258 values y is the average of 10.2461 and 6.0926).
Thanks in advance

Best Answer

x1 = floor(x(:))+1;
[ii,g] = findgroups(x1);
out = [g,splitapply(@mean,y(:),ii)];
Second variant of the output.
x1 = floor(x(:))+1;
[ii,g] = findgroups(x1);
xx = splitapply(@mean,y(:),ii);
out = [x(:),xx(ii)];