没有例子很难回答,但听起来你只想表演
montecarlo simulation?
假设您的形状由函数f定义,并且您具有存储在两个元素向量中的X,Y限制,例如xlim = [-10 10],即这个形状的所有可能的x值都在x = -10和x = 10之间,那么如果没有特定x-y对的值,我建议你使f返回某种错误代码.我将假设那将是NaN.所以f(x,y)是你正在编写的一个函数,如果它可以返回一个z,如果不能则返回NaN
n= 10000;
counter = 1;
shape = nan(n, 3)
while counter < n
x = rand*diff(xlim) + mean(xlmin);
y = rand*diff(ylim) + mean(ylim);
z = f(x,y)
if ~isnan(z)
shape(counter, :) = [x, y, z];
counter = counter + 1
end
end
因此,上面的代码将生成10000(非唯一,但很容易适应)点在您的形状上随机采样.
现在输入后我意识到也许你的形状实际上并不是那么大,也许你可以统一采样而不是随机:
for x = xlim(1):xstep:xlim(2)
for y = ylim(1):ystep:ylim(2)
shape(counter, :) = [x, y, f(x,y)];
end
end
或者如果你写f来进行矢量化(最好)
shape = [(xlim(1):xstep:xlim(2))', (ylim(1):ystep:ylim(2))', f(xlim(1):xstep:xlim(2), ylim(1):ystep:ylim(2));
然后两种方式
shape(isnan(shape(:, 3), :) = []; %remove the points that fell outside the shape