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)?
For example considering the following 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 ]
]
]
...where A
is the origin and B
is the destination of the vector. Can write:
shape = [2, 3, 2]
vector = [1, 0, 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 ]
]
We can write this:
shape = [2, 5]
vector = [1, 0]
And, once again,
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.