Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you are probably looking for is the <a href="http://en.wikipedia.org/wiki/Cantor_pairing_function" rel="nofollow">Cantor pairing function</a>. It is defined for NxN, so you can use it for arbitrarily large dimensions. As mentioned on the wikipedia page it can be inductively generalized to an array of dimension n, in your case n=3 works just fine. </p> <p>The above page also explains inverting the function, so you can get your coordinates in the array from the given number, which is exactly what you want with <code>nthElement</code>.</p> <p>Of course, Cantor has only defined one possible way to walk through a two-dimensional field. There are other possible walks, but this one is the most popular way to do it.</p> <p>Edit: I should mention, that if your array has rectangular dimensions the Cantor pairing function would assume dimensions of the largest kind. So the associated numbers are no longer successively within your array. F.ex. an array of size 1000x2 would be treated as a 1000x1000 array and the numbers corresponding to entries in your actual array would only be a non-successive list of the numbers 0..1000*1000. If, however, your three dimensions are always the same, then this point can be totally ignored.</p> <p>In response to the comment: Row-by-row and Cantor pairing are just different ways to walk through your matrix space. An advantage of Cantor pairing is that it is defined over the natural numbers and hence also is applicable if you do not have exact values for your matrix dimensions, or your matrix grows over time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload