# create a 3D average curve from two 3D curves

2 views (last 30 days)
Alberto Acri on 31 May 2024
Commented: Mathieu NOE on 31 May 2024
I have curves M1 and M2 composed of x nodes in space.
Is it possible to create an average curve (up to a specific height, from bottom to top - red curve) as in the figure?
figure
plot3(M1(:,1),M1(:,2),M1(:,3),'mo','Markersize',4);
hold on
plot3(M2(:,1),M2(:,2),M2(:,3),'go','Markersize',4);
hold off
axis equal
xlabel('x')
ylabel('y')
zlabel('z')
grid off

Mathieu NOE on 31 May 2024
hello Alberto and welcome back
we need to sort and remove duplictes in your data before creating a common z vector, then interpolate both x,y datas on this new reference
then as we have now a common z axis (zm indeed) , we can sum the x and y datas
the new curve is in black
x1 = M1(:,1);
y1 = M1(:,2);
z1 = M1(:,3);
x2 = M2(:,1);
y2 = M2(:,2);
z2 = M2(:,3);
% remove duplicates and sort z data
[z1,ia,ic] = unique(z1);
x1 = x1(ia);
y1 = y1(ia);
[z2,ia,ic] = unique(z2);
x2 = x2(ia);
y2 = y2(ia);
% ctreate new common z array
zmin = max([min(z1);min(z2)]);
zmax = min([max(z1);max(z2)]);
zm = linspace(zmin,zmax,200); % common z value
% interpolate x,y data
x1i = interp1(z1,x1,zm');
y1i = interp1(z1,y1,zm');
x2i = interp1(z2,x2,zm');
y2i = interp1(z2,y2,zm');
% mean curve
xm = (x1i+x2i)/2;
ym = (y1i+y2i)/2;
figure
plot3(x1,y1,z1,'mo','Markersize',4);
hold on
plot3(x2,y2,z2,'go','Markersize',4);
plot3(xm,ym,zm,'k*','Markersize',4);
hold off
axis equal
xlabel('x')
ylabel('y')
zlabel('z')
grid off
Alberto Acri on 31 May 2024
Thanks! :)
Mathieu NOE on 31 May 2024
as always, my pleasure !

### Categories

Find more on Interpolating Gridded Data in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!