本文介绍了向量转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我们如何才能将其转换成一维的新载体(由扁平化多维数组)了解向量的多维数组的形状?

Knowing the multidimensional-array's shape of a vector, how can we convert it into a new vector of one dimension (by flatten the multidimensional-array)?

例如考虑以下数组:

arr = [
  [
    [ nil, nil ],
    [ nil, nil ],
    [ nil, nil ]
  ],
  [
    [ nil, nil ],
    [ nil, nil ],
    [ nil, nil ]
  ]
]

arr[0][0][0] = "A"
arr[1][0][1] = "B"

arr # =>
[
  [
    [ "A", nil ],
    [ nil, nil ],
    [ nil, nil ]
  ],
  [
    [ nil, "B" ],
    [ nil, nil ],
    [ nil, nil ]
  ]
]

...其中 A 是起源和 B 为载体的目的地。可以这样写:

...where A is the origin and B is the destination of the vector. Can write:

shape  = [2, 3, 2]
vector = [1, 0, 1]

从现在开始,我们假设压扁改编,我们怎么能转化载体?换句话说,如何3维此载体转化为1维的一个新的?

From now, supposing we flatten arr, how could we translate the vector? In other words, how to translate this vector of 3 dimensions into a new one of 1 dimension?

这是一个特殊的情况下,由于该矢量的原点也是阵列的第一坐标。因此,我们可以发现,其结果:

This is a special case, because the origin of the vector is also the first coordinate of the array. So we can found the result with:

arr.flatten.index("B") # => 7

下面是一个带有二维数组另外一个例子:

Here's a another example with a 2D-array:

arr = [
  [ "A", nil ],
  [ "B", nil ],
  [ nil, nil ],
  [ nil, nil ],
  [ nil, nil ]
]

我们可以这样写:

shape  = [2, 5]
vector = [1, 0]

和,再次

arr.flatten.index("B") # => 2

但这里是一个比较复杂的例子,有负向量:

But here is a more complicated example, with a negative vector:

arr = [
  [ "B", nil ],
  [ "A", nil ],
  [ nil, nil ],
  [ nil, nil ],
  [ nil, nil ]
]

shape  = [2, 5]
vector = [-1, 0]

如何能在下面的方法可以写?

How can the following method can be written ?

vector2index(shape, vector) # => -2

与一维数组的一个例子(简单):

An example (simple) with a 1D-array:

arr = [ nil, "B", nil, nil, "A", nil, nil ]

shape  = [7]
vector = [-3]
vector2index(shape, vector) # => -3

有一个简单的方法来平坦的载体从任何尺寸的阵列?谢谢你。

Is there a simple way to flat a vector from an array of any dimensions? Thanks.

推荐答案

首先,假设阵列的第一个元素为X轴,第二 - 为Y轴,第三 - 为Z轴,则必须在第二个错误和第三示例。第三个例子应该是

First of all, assuming that first element of array is for X axis, second - for Y axis, third - for Z axis, you have a mistake in second and third example. Third example should be

shape  = [2,5]
vector = [0,-1]
vector2index(shape, vector) # => -2

和如果数组的第一元素为Y轴,第二 - 为X轴,然后加入2-次和第3次实施例是正确的,但第一个例子是错误的

And if first element of array is for Y axis, second - for X axis, then 2-nd and 3-rd examples is correct, but the first example is wrong.

如果我理解正确的想法,我们需要在第一个例子中的乘法矢量[1] 定型[0] ,乘矢量[2] 塑造[0] *形状[1] ,然后计算3元素的总和。一般情况下,我们并不需要乘以0个元素,我们需要n个元素相乘以塑造[0] *形状[1] * ... *形状[N-1]

If I understand the idea correctly, we need in first example multiply vector[1] to shape[0], multiply vector[2] to shape[0]*shape[1] and then calculate sum of 3 elements. Generally, we don't need to multiply 0-th element, and we need to multiply n-th element to shape[0]*shape[1]*...*shape[n-1].

您可以实现这种方式:

vector.each_with_index.map {
  |v, i| i == 0? v: v * shape[0..i-1].inject(:*)
}.inject(:+)

UPD。的你更新你的问题后,它变得更加清晰。如果您想preserve Ruby的索引顺序,你需要扭转两个数组形状矢量

Upd. After you updated your question, it becomes more clear. If you want to preserve Ruby's indexing order, you need to reverse both arrays shape and vector.

vector.reverse.each_with_index.map {
  |v, i| i == 0? v: v * shape[0..i-1].reverse.inject(:*)
}.inject(:+)

这篇关于向量转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:22