给你个例子:
clc; clear;
n = 201;
x = linspace(-2, 2, n);
y = x;
[X, Y] = meshgrid(x, y);
z = sin(X.^2+Y.^3);
x1 = -2; y1 = -2; x2 = 2; y2 = 2;
n = 201;
xx = linspace(x1, x2, n);
k = (y2-y1) / (x2-x1);
b = y1 - k * x1;
yy = k * xx + b;
v = zeros(1, n);
d0 = 0.05;
for i = 1: n
xi = xx(i); yi = yy(i);
d = sqrt((xi-X).^2+(yi-Y).^2);
vv = z(d<=d0);
v(i) = mean(vv);
end
figure('position', [100, 500,1000, 350]);
subplot(121);
colormap(jet);
[~, c] = contourf(x, y, z, 51);
hold on;
plot(xx, yy, 'linewidth', 2, 'color', 'w');
set(c, 'color', 'none');
colorbar();
xlabel('x'); ylabel('y');
subplot(122);
plot(xx, v, 'b');
xlabel('x');
ylabel('v');