假设我有一个x坐标的向量,一个y坐标的矩阵和相应的z值:
xcoordinates = [1 2 3 4 5];
ycoordinates = repmat(xcoordinates,5,1)+rand(5,5);
z = zeros(5,5);
for x=xcoordinates
for y=1:5
z(x,y) = sqrt(x^2+ycoordinates(x,y)^2);
end
end
如何在相应的 x 和 y 值给定的点处绘制由 z 值确定的表面?第一个 x 值定义矩阵第一行中所有 y 值的 x 值,第二个 x 值定义第二行中所有值的 x 值,依此类推。
(如果答案是griddata
我想要一些额外的指示。如何使数据采用正确的格式?
mesh(repmat(xcoordinates,5,1), ycoordinates, z)
顺便说一下,您可以轻松地对此计算进行矢量化:
x = repmat(1:5, 5, 1);
y = x + rand(5,5);
z = sqrt(x.^2+y.^2);
mesh(x', y, z)