找出“扁平化索引”来自“堆叠索引”数组的
我试图提出一个方程,从“堆叠索引”中以数学方式确定数组的“扁平索引”。请观察以下 Ruby 示例。
matx = [[[ 1, 2, 3, 4],
[ 5, 6, 7, 8]],
[[ 9,10,11,12],
[13,14,15,16]]]
在此示例中,matx
是一个三维矩阵,元素 7
位于 matx[0][1][2]
。然而,在下一个示例中:
matx.flatten! # => [1, 2, 3, 4, 5, 6, 7, 8,
# 9, 10, 11, 12, 13, 14, 15, 16]
现在元素 7
位于 matx[6]
。
所以本质上,我正在寻找一种方法,在给定矩阵的维度和特定元素的索引集的情况下,从堆叠矩阵转换为展平矩阵。 Reverse 也很棒,但我认为获得该结果的方法与获得此结果的方法类似(但本质上是相反的)。 我意识到 reverse 实际上并不是一个函数,因为没有一种必然能够区分 5 映射到 [2,3] 还是 [3,2] 等的方法。所以我不打算研究这个。
I'm trying to come up with an equation to mathematically determine the "flattened index" of an array from the "stacked index." Observe the following example in Ruby.
matx = [[[ 1, 2, 3, 4],
[ 5, 6, 7, 8]],
[[ 9,10,11,12],
[13,14,15,16]]]
In this example, matx
is a three dimensional matrix, and the element 7
is located at matx[0][1][2]
. However, in the next example:
matx.flatten! # => [1, 2, 3, 4, 5, 6, 7, 8,
# 9, 10, 11, 12, 13, 14, 15, 16]
Now the element 7
is located at matx[6]
.
So essentially, I'm looking for a way to, given the dimensions of the matrix and the set of indices for the particular element, convert from the stacked matrix to the flattened matrix. Reverse would be awesome, too, but I figure the way to get that is similar (but essentially reversed) to the method of obtaining this result. I realized that reverse is not actually a function, because there's no way to necessarily tell the difference as to whether 5 maps to [2,3] or [3,2], etc. So I'm not going to look into that one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)