Matrix Value Loop

I have a group of values ​​and I need specific output:

i in the table these values:

id | value 1 | 1/4 2 | 5/1 3 | 4/1 4 | 6/1 5 | 2/1 6 | 3/1 

What am I trying to do:

  A | B | C | D | A | 1 | 1/4 | 5/1 | 4/1 | B | | 1 | 6/1 | 2/1 | C | | | 1 | 3/1 | D | | | | 1 | 

I have only 6 values, and I am making an output with 10 values

:

 $matrix[0][0] = 1/1; $matrix[0][1] = 1/4; $matrix[0][2] = 5/1; $matrix[0][3] = 4/1; $matrix[1][1] = 1/1; $matrix[1][2] = 6/1; $matrix[1][3] = 2/1; $matrix[2][2] = 1/1; $matrix[2][3] = 3/1; $matrix[3][3] = 1/1; 

I am trying to do something like this, but without success:

 while ($sql -> fetch()) { for ($i = 0; $i < (//something); $i++) { for ($x = 0; $x < (//something); $x++) { $matrix[$i][$i+1] = $result; } } } 

any idea? thanks

+4
source share
1 answer

Try to implement this in php. below is the java code for your logic.

 Integer[] singleDim = new Integer[] {2,3, 2}; int matrixSiz = 3; //this value has to be set manually. //i didnt get time to find the logic for this Integer[][] twoDim = new Integer[matrixSiz][matrixSiz]; int counter = 0; //this iterates through singleDim array //logic being done for convesion for (int i = 0; i < matrixSiz; i++) { for (int j = 0; j < matrixSiz; j++) { if (i==j) { twoDim[i][j]=1; //setting the diagonal as 1 }else if (j>i){ // important condition to find the upper part of the matrix twoDim[i][j]=singleDim[counter++]; } } } //printing it. for (int i = 0; i < matrixSiz; i++) { System.out.println(); //new line for (int j = 0; j < matrixSiz; j++) { if(twoDim[i][j]==null) System.out.print("\t"); else System.out.print("\t"+twoDim[i][j]); } } 

Output

 1 2 3 1 2 1 

The above logic should work by modifying singleDim and matrixSiz for any data.

0
source

All Articles