如何从meshgrid正确获取节点计数?

时间:2017-03-03 06:19:55

标签: arrays matlab

假设我有以下向量:

x = [1 2 3 4];
y = [4 5 6];
z = [7 8 9];

我想将这些合并到网格中,然后获取x,y,z坐标以及nodeCount。我有代码来自:How do I obtain the count of corner points in a matrix as well as store their positions in an array?

如下:

ny = length(y) - 1;
nx = length(x) - 1;
nz = length(z) - 1;

%# Obtain grid vectors
[ygv, xgv, zgv] = meshgrid(1:ny, 1:nx, 1:nz); 
coords = [ygv(:), xgv(:), zgv(:)]

%# Obtain node counts
nodeCount = ny*nx*(coords(:,3) - 1) + nx*(coords(:,2) - 1) + coords(:,1)

%# Obtain coordinates
x_coord = mod(nodeCount - 1, nx) + 1;
y_coord = mod((nodeCount - x_coord)/nx, ny) + 1;
z_coord = (nodeCount - x_coord - nx*(y_coord - 1))/nx/ny + 1;

我的预期结果是:

coords = [1 1 1 
         1 2 1 
         1 3 1 
         2 1 1
         2 2 1
         2 3 1
         1 1 2
         1 2 2
         1 3 2
         2 1 2
         2 2 2
         2 3 2]

nodeCount = [1;2;3;4;5;6;7;8;9;10;11;12]

y_coord = [1;1;1;2;2;2;1;1;1;2;2;2]
x_coord = [1;2;3;1;2;3;1;2;3;1;2;3]
z_coord = [1;1;1;1;1;1;2;2;2;2;2;2]

但是,代码只会返回cords, ygv, xgv, and zgv的正确结果,但不会返回其他结果。我理解nodeCount算不上。 coords中的行数。无论如何,我怀疑nodeCount公式可能是错误的,这会影响后续的计算。请问有没有按预期获得nodeCount的方法?谢谢!

1 个答案:

答案 0 :(得分:0)

糟糕!我只知道错误与我的坐标排序有关。考虑y,x,z排序的正确nodeCount公式应为:

nodeCount = ny*nx*(coords(:,3) - 1) + nx*(coords(:,1) - 1) + coords(:,2)

有了它,其他所有事情都有效。

相关问题