Recursive or loop method to popular or list an n-dimensional array

Asked

Viewed 460 times

3

I’m trying to build a method capable of filling an n-dimensional matrix in all positions, for example: I have a 2-dimensional matrix, where this matrix is 2x2 with only 2 dimensions in a Cartesian plane, for example.

I would like the same method to fill a matrix with 3 dimensions yet 2x2, however I would not like to put fixed in the code in the case of loop two-dimensional one inside the other and when it is three-dimensional 3 also one loop inside each other. I need to use the same multidimensional code, for example: 8 dimensions with a 2x2 matrix.

My 2D example:

int matrix2d [] [] = new int [2] [2];
for (int i = 0; i <matrix2d.length; i ++) {
    for (int j = 0; j <matrix2d [i] .length; j ++) {
        System.out.println ("(" + i + "," + j + ")");
    }
}

Result: (0.0) (0.1) (1.0) (1,1)

My 3D example:

int matrix3d [] [] [] = new int [2] [2] [2];
for (int i = 0; i <matrix3d.length; i ++) {
    for (int j = 0; j <matrix3d [i] .length; j ++) {
        for (int l = 0; l <matrix3d [i] [j] .length; l ++) {
            System.out.println ("(" + i + "," + j + "," + l + "));
        }
    }
}

Result: (0.0.0) (0.0.1) (0.1.0) (0.1,1) (1.0.0) (1.0,1) (1,1,0) (1,1,1)

I would like to have a method or loop generic, not needing to include a loop each time the size increases.

  • Have you tried linearizing your matrix?

  • https://stackoverflow.com/a/14773536/5524514

  • I’ve done it, more would need as the posted indices of the results.

2 answers

5

It is very difficult to have real problems with more than 3 or 4 dimensions. If you have a problem like this or it is set wrong or it is better to find a different solution.

Even if you have a problem like this it grows exponentially and will need a lot of efficiency. This generalization will create a overhead on the run to control the loop that counts the dimensions, which is a heavier weight on something that will become very heavy.

You can do it, but the correct engineering of this is not to do it. Better create the individual and specialized case for the dimensions that will actually be used.

It may not be the answer I was hoping for, but it’s what you should do. Developing software is not making the smallest code, it is making the best code that solves the problem.

  • It is not difficult no, in machine learning we have a lot, I am with a 6 dimensions for example. Thank you for the contribution.

  • It’s hard, you cited 1 case where it’s used. And one case where this abstraction is not desired and what I said to do will work much better.

2


Since I don’t know the purpose of the code, here’s a very basic way to create the matrix points, using recursion:

private static int numLinha;

public static void main (String[] args) throws java.lang.Exception
    {
    int dims = 5;
        int size = 3;
    numLinha = 0;
    getMatrix(dims, size);
}

private static void getMatrix(int dims, int size) {
    int[] arr = new int[dims];
    getMatrixPoint(0, 0, dims, arr, size);
}

private static void getMatrixPoint(int pos, int index, int currDim, int[] arr, int size) {

    //condicao de fim de recursao
    if (currDim == 0) {
        String strPoint = "(";
        String sep = "";
        for (int i = 0; i < arr.length; i++) {
            strPoint = strPoint + sep + arr[i];
            sep = ", ";
        }
        strPoint = strPoint + ")";
        System.out.println(++numLinha + " - " + strPoint);
        return;
    }

    //para cada possivel valor da primeira dimensao
    //calcule os possiveis pontos da (quantidade_de_dimensoes - 1)
    for (int i = 0; i < size; i++) {
        arr[pos] = i;
        getMatrixPoint(pos + 1, i, currDim - 1, arr, size);
        arr[pos] = 0;
    }
}

For an array of size and number of dim dimensions, size dims will be generated. See on Ideone

  • I will try to apply this to my problem, actually I have a grouping algorithm that will do the following, I will need to assemble a matrix and check if inside it, there are points, however, I can have the matrix in n dimensions, so without knowing what would be the input of the problem, I need to have the matrix assembled in n dimension. Thanks for the contribution, I will try to apply in my problem and I return here to comment.

  • It worked, I used this return to create my algorithm, thank you very much, I’m only with another problem now and I will try to change the code you gave me, in this case the matrix is square ex: 5x5, I need to get when you pass me a non-square value ex: the input of 25 cells, would be done the algorithm with 5x5 = 25 cells; however, the algorithm can receive non-square sizes, eg: 8 cells and in this case should assemble a matrix 4x2, 2x4, or even an input 5 cells that should assemble a matrix 5x1.

  • In this case, from "top of my head", you could pass information arrays, i.e., pos arrays, index, currDim and size (arr[] remains an array of the coordinates of the point, incidentally point would be a better name for the variable).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.