【问题标题】:calculating 3D coordinates from a flattened 3D array从展平的 3D 数组计算 3D 坐标
【发布时间】:2018-07-03 15:12:35
【问题描述】:

我有一个线性索引,按列主要顺序展平,我想取回 3D 坐标 [x,y,z]。我在行专业 https://math.stackexchange.com/questions/19765/calculating-coordinates-from-a-flattened-3d-array-when-you-know-the-size-index 找到了这个,但无法弄清楚列专业?

【问题讨论】:

标签: c++ matrix coordinates linear-algebra


【解决方案1】:

给定

sometype array[XSIZE][YSIZE][ZSIZE];

然后作为一维数组,那么如果你有 x >= 0 且 x = 0 且 y = 0 且 z

Index = ((x * YSIZE + y) * ZSIZE) + z;      // Row major order, C/C++
Index = ((z * YSIZE + y) * XSIZE) + x;      // Col major order

对于计算索引,给定 x, y, z:

// For Row major order
z = Index % ZSIZE;
y = (Index / ZSIZE) % YSIZE;
x = Index / (ZSIZE * YSIZE);

// For Col major order
x = Index % XSIZE;
y = (Index / XSIZE) % YSIZE;
z = Index / (XSIZE * YSIZE);

【讨论】:

  • n维数组有什么通用的公式吗?
  • 是的。给定数组 a[N1][N2}[N3]...[NN} 然后查看一维数组并访问元素 a[m1][m2][m3]...[mn] Index = (((. . (m1 * N2 + m2) * N3 + m3) * N4 + m4) * .... + aN;并且 mN = 索引 % NN; mN-1 = (索引 / NN) % NN-1; : m1 = Index / (NN * N(N-1) * N(N-2) * N2); // 其中 (N-n) 被读作下标
猜你喜欢
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多