Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

java - 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]+"");        
            }   
        }
        System.out.println();
    }

but I didn't get the desired result.

question from:https://stackoverflow.com/questions/65882075/creating-a-3d-matrix-from-2d-matrix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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] + "]";
    }
 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...