Finding neighbour values average of the 2d data set

2 views (last 30 days)
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

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 1 Mar 2018
Edited: Andrei Bobrov on 1 Mar 2018
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)];

More Answers (2)

KSSV
KSSV on 1 Mar 2018
Read about knnsearch. With this you will get indices of neighbors...with the indices, you can find average.

Image Analyst
Image Analyst on 6 Mar 2018
What you have is a 1-D case, not a 2-D case. For every x value, you have a corresponding y value, so that is a 1-D case. So you can simply use interp1() to get the mean y value between known pairs of x values:
xMiddle = (x(1:end-1)+x(2:end))/2
yMeans = interp1(x, y, xMiddle);
It's as simple as that. Here is the complete script (with setup, comments, fancy plotting, etc.):
% Set up sample data.
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]
% Now get the mean y values
xMiddle = (x(1:end-1)+x(2:end))/2
yMeans = interp1(x, y, xMiddle);
% Plot/visualize the results.
plot(x, y, 'bo-', 'LineWidth', 2, 'MarkerSize', 15);
hold on;
plot(xMiddle, yMeans, 'r*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!