 # NArray - from coordinates to index

Hi,

How can we convert a coordinates to an index?

I’m using the awesome NArray library with some arrays of 1, 2, 3, or
more dimensions. For instance, if we consider this 3x3 array of 2
dimensions …

``````NArray.int(3,3):
[ [ 0, 0, 0 ],
[ 0, 0, 0 ],
[ 0, 0, 0 ] ]
``````

… the square of the coordinate [0,1] have also the index of 3 (and
according to the NArray specs, a is same as a[0,1] if a is 3x3
array).

Is there a simple way to find the index, from the coordinates?

Many thanks,

Sa-

On Jan 8, 2012, at 13:15 , Satsou Sa wrote:

`````` [ 0, 0, 0 ],
[ 0, 0, 0 ] ]
``````

… the square of the coordinate [0,1] have also the index of 3 (and
according to the NArray specs, a is same as a[0,1] if a is 3x3
array).

Is there a simple way to find the index, from the coordinates?

Use the coordinates as a base N number to convert to a base 10 index:

a[0,1] == 10b3 == 13^1+03^0 == 13+01 == 3

index = col_index + row_index * num_cols

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.