user14053977
user14053977

Reputation: 151

Creating a 3d matrix from 2d matrix

I have the Following 2d matrix :

int[][] states=new int[][]{{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};

I want getting a 3d matrix with this output :

[0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1]

I tried the following code :

int[][] states=new int[][]{{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
int[][][] actions = new int[states.length][states.length][2];
// Remplissage :
for (int i = 0; i < states.length; i++) {
  int k = 0;
  for (int j = 0; j < states.length; j++) {
    for (int c = 0; c < states[i].length; c++) {
      actions[i][j] = states[j];
      System.out.print(actions[i][j][c] + "\t");
    }
  }
  System.out.println();
}

but I didn't get the desired result.

Upvotes: 0

Views: 111

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Here's the result of one of my test runs.

[0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,1] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,2] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [2,0] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,1] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,2]
[0,0] | [0,1] | [0,2] | [1,0] | [1,1] | [1,2] | [2,0] | [2,1]

I broke the problem into pieces. First, I created the three-dimensional matrix. You need a nested for loop where you skip each element in turn.

Then I printed out the results of the three-dimensional matrix. I started with one pair. I wrote a method to produce [0,0]. Then, I wrote a method to produce one line of the output. Finally, I wrote a method to produce the entire matrix.

Here's the complete runnable example.

public class CreateMatrix {

    public static void main(String[] args) {
        CreateMatrix cm = new CreateMatrix();
        
        int[][] states = new int[][] { { 0, 0 }, { 0, 1 }, { 0, 2 }, 
            { 1, 0 }, { 1, 1 }, { 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } };
        int[][][] actions = cm.createActions(states);
        System.out.println(cm.createTable(actions));
    }
    
    public int[][][] createActions(int[][] states) {
        int[][][] actions = new int[states.length][states.length - 1][2];
        
        for (int i = 0; i < states.length; i++) {
            int k = 0;
            for (int j = 0; j < states.length; j++) {
                if (i != j) {
                    actions[i][k][0] = states[j][0];
                    actions[i][k][1] = states[j][1];
                    k++;
                }
            }
        }
        
        return actions;
    }
    
    public String createTable(int[][][] actions) {
        String output = "";
        
        for (int i = 0; i < actions.length; i++) {
            output += createLine(actions[i]);
            output += System.lineSeparator();
        }
        
        return output;
    }
    
    private String createLine(int[][] line) {
        String output = "";
        
        for (int i = 0; i < line.length; i++) {
            output += createPair(line[i]);
            if (i < (line.length - 1)) {
                output += " | ";
            }
        }
        
        return output;
    }
    
    private String createPair(int[] pair) {
        return "[" + pair[0] + "," + pair[1] + "]";
    }
 
}

Upvotes: 1

Related Questions