get the coordinates of a matrix from its flatten index

How can we get the coordinates of a *n* dimensions matrix from its shape and its flatten index?

I mean, if for example I have the following (2,3) matrix of 2 dimensions:

Code:

`[ [ 0, 1 ], `

[ 2, 3 ],

[ 4, 5 ] ]

...and I want to find the value of the index in red from the coordinates [0,2], how can I do?

Or if I have this (2,2,5) matrix of 3 dimensions:

Code:

`[ [ [ nil, nil ], `

[ nil, nil ] ],

[ [ nil, nil ],

[ nil, nil ] ],

[ [ nil, 9 ],

[ nil, nil ] ],

[ [ nil, nil ],

[ nil, nil ] ],

[ [ nil, nil ],

[ nil, nil ] ] ]

...and I know the coordinates that I want have a flatten index value of 9, how can I find the relative coordinates are: [1,0,2]?

If possible, I would like to know a general and simple method, which work on matrix of any shape.

Many thanks for your help.