# How to convert a coordinate in an $N$-dimensional cuboid to a unique number?

I got an $N$-dimensional cuboid ($N>1$) with different edge lengths. How can I convert the position of a point into a number, so it's unique and "dense"?

For example, the edge sizes of a 4-dimensional cuboid are:

$$(20, 3, 5, 6)$$

Now the edge lengths can take up these values:

$$0..19, 0..2, 0..4, 0..5$$

Here I have a point, for example: $$(14, 3, 1, 3)$$ How can I convert this to a single, unique number (let's call it index)?

Criteria for the conversion:

1) The space for these indexes should be "dense" (every number between $0$ and $1799$ should represent a coordinate)
2) Two different coordinates should never translate to the same index.
3) All indexes will be between $0$ and $20\cdot 3 \cdot 5\cdot 6 - 1 = 1799$.

We can assume that all numbers are integers.

• Are the points only integer coordinates? Oct 26, 2017 at 16:44
• @frogeyedpeas yes
– sydd
Oct 26, 2017 at 16:46
• Let $M$ be an integer greater than all your edge lengths. Then let $(c_1, c_2, c_3, c_4)$ be a point we have a unique number $M^3 (c_1) + M^2 (c_2) + M(c_3) + c_4$ we can identify it with Oct 26, 2017 at 16:48
• @frogeyedpeas That does not satisfy criteria #2 and #3.
– sydd
Oct 26, 2017 at 17:10
• Ah, I missed those Oct 26, 2017 at 17:10

## 2 Answers

You can just use the way computers store arrays. Take $(i,j,m,n)$ to $i+20j+20\cdot 3m + 20\cdot 3 \cdot 5n$

• Yep, I am looking for something like this...Just dont see why its correct.
– sydd
Oct 26, 2017 at 17:57
• Try it by hand for three dimensions, say $2 \times 3 \times 4$ and you will see. It is just the reverse of the dictionary order. $n$ is the most important coordinate, then $m$ and so on. Oct 26, 2017 at 18:05

Each lattice point belonging to your cuboid is encoded as an integer quadruple $$(x_1,x_2,x_3,x_4)\in[0..20]\times[0..3]\times[0..5]\times[0..6]\ .$$ Since there are $21\cdot4\cdot6\cdot 7=3528>1800$ admissible quadruples there is no bijective numbering of these quadruples using only the numbers in $[0..1800]$.

• Oops sorry the example is not clear. The edge coordinates in the example are between ´0..19, 0..2, 0..4, 0..5´, so their product is 1800.
– sydd
Oct 26, 2017 at 17:41