How to convert a monodimensional index to corresponding indices in a multidimensional array?

Say I have a 3x4x5x6 java double array a that I unroll into an ArrayList b of length 360 in the following way:

for (int i = 0; i<a.length; i++){
    for (int j = 0; j<a[0].length; j++){
        for (int k = 0; k<a[0][0].length; k++){
            for (int m = 0; m<a[0][0][0].length; m++){
                b.add(a[i][j][k][m]);
            }
        }
    }
}

Given the index of b, is there an easy way to find the corresponding 4-tuple of indices in a?

Answer:1

Assuming that

  • b is the index on the monodimensional array
  • i,j,k,m are the four resulting indices on the multidimensional array
  • si,sj,sk,sm are the size of any dimension

you can use basic math, it should be something like

  • m = b % sm
  • k = (b / sm) % sk
  • j = (b / (sm*sk)) % sj
  • i = b / (sm*sk*sj)

Basically you increment every index by one for every size of the contained arrays (by multiplying sizes) and you wrap it on its dimension.

Answer:2

I am basically making a battleship guessing game where you have to the position of a ship by the click of your mouse. When a position of the ship is guessed correctly it deletes that ship cell from ...

Estoy tratando de instalar el marco de extracción DBPedia siguiendo el http://wiki.dbpedia.org/Documentation He descargado la versión binaria de Maven. $ mvn --version Apache Maven 3.0.4 (r1232337; ...

Tengo un problema al convertir el byte al archivo de sonido .mp3. En mi caso, lo hago usando FileOutputStream usando su método de escritura (bytes) pero solo crea un archivo de datos con extensión mp3 pero no puedo reproducirlo en ...

Hola, estoy haciendo esto en el método public void update (Table table, String tableName) {ArrayList <Name> firstRowInDslFormat = new ArrayList <> (); para (Valor del objeto: tabla ...