[英]Creating a 3d matrix from 2d matrix
我有以下二維矩陣:
int[][] states=new int[][]{{0,0},{0,1},{0,2},{1,0},{1,1},{1,2},{2,0},{2,1},{2,2}};
我想用這個 output 得到一個 3d 矩陣:
[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]
我嘗試了以下代碼:
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();
}
但我沒有得到想要的結果。
這是我的一次測試運行的結果。
[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]
我把問題分解成碎片。 首先,我創建了三維矩陣。 您需要一個嵌套的 for 循環,依次跳過每個元素。
然后我打印出三維矩陣的結果。 我從一對開始。 我寫了一個方法來產生[0,0]
。 然后,我寫了一個方法來產生一行output。 最后,我寫了一個方法來產生整個矩陣。
這是完整的可運行示例。
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] + "]";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.