0
假設我有以下的載體:如何從meshgrid正確獲取節點數?
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
?謝謝!