我有一个关于 Matlab 处理坐标 (0,0) 替换的问题。我知道如果我将值 (0,0) 分别代入
fx
和 fy
中,就会出现错误。那么有哪些语法可以解决将 (0,0) 替换为 (10^-5,10^-5) 的问题呢?
此问题详情如下:
我的代码:
[x,y] = meshgrid(-5:0.1:5,-5:0.1:5);
z = exp(-x.^2-y.^2)
figure(1),surf(x,y,z)
fx =((x)./(x.^2+y.^2)^0.5);
fy =((y)./(x.^2+y.^2)^0.5);
x0 = 0; % desired location to be found in x
y0 = 0; % desired location to be found in y
tol = 1e-5 % Numerical tolerance
% Create a mask for points within the tolerance distance on both x and y
mask = (abs(x - x0) < tol ) & (abs(y - y0) < tol );
new_coord = 1e-5; % New coordinate
% Replace found locations
x[mask] = new_coord;
y[mask] = new_coord;
我使用数字容差来搜索位置,因为使用直接比较可能会导致没有位置匹配。请参阅 为什么 MATLAB 中 24.0000 不等于 24.0000? 以供参考。