Octave Error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)

huangapple go评论45阅读模式
英文:

Octave Error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)

问题

Octave在尝试对矩阵进行网格化时显示了上述错误:

错误:surface:行(Z)必须与长度(Y)相同,列(Z)必须与长度(X)相同

我正在尝试使用mesh函数对矩阵N进行网格化,但一直出现上述错误。我已经使用length命令检查了X、Y和Z的大小,它们都是完全相同的大小。所以我不明白错误背后的原因。

另外,我已经尝试将命令mesh(tx,ty,tz)更改为mesh(x,y,tz),但问题仍然存在。

附注:我使用了Octave的参考页面来学习有关mesh命令的信息,链接如下:https://docs.octave.org/v6.2.0/Three_002dDimensional-Plots.html 并且还检查了示例给出的网格化代码。

如果有人能回答我,我提前表示感谢。

英文:

Octave shows this error when trying to mesh a matrix:

>error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)*

N =

        0        0   0.0057
   1.0000        0   0.0165
   1.0000   1.0000   0.0145
        0   1.0000   0.0038
   0.5000   0.5000        0
   0.6667   0.5000   0.0001
   0.3333   0.5000   0.4560
   0.6443   0.5833   0.8280
   0.5833   0.6443   0.5774
   0.5000   0.6667   0.0000
   0.4167   0.6443   0.0001
   0.3557   0.5833   0.5898
   0.3557   0.4167   0.6540
   0.4167   0.3557   0.6016
   0.5000   0.3333   0.0000
   0.5833   0.3557   0.0001
   0.6443   0.4167   0.7667
   0.5000   0.2083   0.1617
   0.2083   0.5000   0.1576
   0.5000   0.7917   0.0048
   0.7917   0.5000   1.0050
   0.2961   0.7039   1.0004
   0.7039   0.2961   1.0027
   0.7039   0.7039   1.0024
   0.2961   0.2961   1.0008

tx= N(:,1);
ty= N(:,2);
tz= N(:,3);

length(tx);
length(ty);
length(tz);

figure(1)
[x,y] = meshgrid (tx, ty);
mesh(tx,ty,tz);
xlabel ("tx");
ylabel ("ty");
zlabel ("tz");

I am trying to mesh the matrix N using mesh but it keeps showing up the above error. I've already checked the size of X, Y and Z using the command length and it they all are the exact same size. So I don't understand what is the reason behind the error.

Also I have tried changing the command mesh(tx,ty,tz) to mesh(x,y,tz) but it is still the same.

Pd: I used the reference page from Octave to learn about the mesh command in this link: https://docs.octave.org/v6.2.0/Three_002dDimensional-Plots.html and also checked the code for the mesh of the example given.

Thanks in advance if someone can answer me.

答案1

得分: 1

问题在于这里没有一个表面,而是一个不规则的散点图,带有其上的z值。首先,您需要将可用的z值插值到规则的(x,y)网格上:为此,您可以使用griddata函数:

N = [
    0        0   0.0057
    1.0000        0   0.0165
    1.0000   1.0000   0.0145
    0   1.0000   0.0038
    0.5000   0.5000        0
    0.6667   0.5000   0.0001
    0.3333   0.5000   0.4560
    0.6443   0.5833   0.8280
    0.5833   0.6443   0.5774
    0.5000   0.6667   0.0000
    0.4167   0.6443   0.0001
    0.3557   0.5833   0.5898
    0.3557   0.4167   0.6540
    0.4167   0.3557   0.6016
    0.5000   0.3333   0.0000
    0.5833   0.3557   0.0001
    0.6443   0.4167   0.7667
    0.5000   0.2083   0.1617
    0.2083   0.5000   0.1576
    0.5000   0.7917   0.0048
    0.7917   0.5000   1.0050
    0.2961   0.7039   1.0004
    0.7039   0.2961   1.0027
    0.7039   0.7039   1.0024
    0.2961   0.2961   1.0008]

tx = N(:,1);
ty = N(:,2);
tz = N(:,3);

txi = linspace(0,1,101);
tyi = linspace(0,1,101);
[x,y] = meshgrid (txi, tyi);
z = griddata (tx, ty, tz, x, y)

figure(1)
mesh(x,y,z);
xlabel ("tx");
ylabel ("ty");
zlabel ("tz");

或者,您可以使用plot3来直接在(x,y,z)空间中绘制散点图。但是在三维空间中绘制浮点数很难可视化,因此可以帮助绘制一些从(x,y)平面到每个点的柱状图:

figure(2)
plot3(tx,ty,tz,'o')
hold on
for k = 1:length(tx)
    plot3([tx(k) tx(k)],[ty(k) ty(k)],[0 tz(k)],'b')
end
hold off
xlabel ("tx");
ylabel ("ty");
zlabel ("tz");

希望这些信息对您有帮助。

英文:

The problem is that you don't have a surface here, but a (x,y) irregular scatter with the z values on it. You need first to interpolate your available z values onto on regular (x,y) grid: for this, you can use the griddata function:

N =[
        0        0   0.0057
   1.0000        0   0.0165
   1.0000   1.0000   0.0145
        0   1.0000   0.0038
   0.5000   0.5000        0
   0.6667   0.5000   0.0001
   0.3333   0.5000   0.4560
   0.6443   0.5833   0.8280
   0.5833   0.6443   0.5774
   0.5000   0.6667   0.0000
   0.4167   0.6443   0.0001
   0.3557   0.5833   0.5898
   0.3557   0.4167   0.6540
   0.4167   0.3557   0.6016
   0.5000   0.3333   0.0000
   0.5833   0.3557   0.0001
   0.6443   0.4167   0.7667
   0.5000   0.2083   0.1617
   0.2083   0.5000   0.1576
   0.5000   0.7917   0.0048
   0.7917   0.5000   1.0050
   0.2961   0.7039   1.0004
   0.7039   0.2961   1.0027
   0.7039   0.7039   1.0024
   0.2961   0.2961   1.0008]

tx= N(:,1);
ty= N(:,2);
tz= N(:,3);

txi = linspace(0,1,101);
tyi = linspace(0,1,101);
[x,y] = meshgrid (txi, tyi);
z =  griddata (tx, ty, tz, x, y)

figure(1)
mesh(x,y,z);
xlabel ("tx");
ylabel ("ty");
zlabel ("tz");

Octave Error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)

Alternatively, you can use plot3 to directly plot your scatter in in the (x,y,z) space. But floating points in a 3D space are difficult to visualize, so it can help for instance plotting also some bars from the (x,y) plane to each point:

figure(2)
plot3(tx,ty,tz,'o')
hold on
for k = 1:length(tx)
    plot3([tx(k) tx(k)],[ty(k) ty(k)],[0 tz(k)],'b')
endfor
hold off
xlabel ("tx");
ylabel ("ty");
zlabel ("tz");

Octave Error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)

huangapple
  • 本文由 发表于 2023年2月24日 07:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定