0

I need to figure out how to turn the 3x3 array

{{7,2,3}, {0,4,8}, {5,6,1}} 

into a 9x9 array where each 3x3 section is the original array o, but each value n is n+(9*c) where c is the corresponding section. In other words, section 0, 0 (top left) should have each of its values changed to originalValue+(9*7) since the top-left section of the original array is 7. Similar with the bottom-right, but the formula would be originalValue+(9*1) since the bottom-right section of the original array is 1. The new array should look like (just including the two sections mentioned)

{{70,65,66,0,0,0,0,0,0}, {63,67,70,0,0,0,0,0,0}, {68,69,64,0,0,0,0,0,0}, {00,00,00,0,0,0,0,0,0},//leading zeros added for easy legibility {00,00,00,0,0,0,0,0,0}, {00,00,00,0,0,0,0,0,0}, {0,0,0,0,0,0,16,11,12}, {0,0,0,0,0,0,09,13,17}, {0,0,0,0,0,0,14,15,10}} 

Now here is the hard part: I then need to repeat the process, but using this 9x9 array as the original array to get a 27x27 array, and then repeat it even more times to get a bigger array each time (81x81 then 243x243, etc.).

I have been able to get the method addAllValues() but I am stuck from going further.

public static int[][] addAllValues(int[][] data, int value2){ int value = value2 * 9; int[][] temp = data.clone(); int[][] newData = temp.clone(); for (int i = 0; i < temp.length; i++) { for (int j = 0; j < temp[0].length; j++) { newData[i][j] = temp[i][j] + value; } } return newData; } 

Can anyone help me with this? Thanks in advance!

Here is what I have tried:

public static int[][] gen(int amount) { if (amount == 0) { return normal; } else { int[][] newArray = gen(amount-1); int[][] temp = new int[newArray.length*3][newArray.length*3]; int[][][][] newArrays = {{addAllValues(newArray,7),addAllValues(newArray,2),addAllValues(newArray,3)},{addAllValues(newArray,0),addAllValues(newArray,4),addAllValues(newArray,8)},{addAllValues(newArray,5),addAllValues(newArray,6),addAllValues(newArray,1)}}; for (int i = 0; i < temp.length; i++) { for (int j = 0; j < temp.length; j++) { int x=0,y=0; if (0 <= i && i < 3){ x = 0; } else if (3 <= i && i < 6){ x = 1; } else if (6 <= i && i < 9){ x = 2; } if (0 <= j && j < 3){ y = 0; } else if (3 <= j && j < 6){ y = 1; } else if (6 <= j && j < 9){ y = 2; } temp[i] [j] = newArrays[x] [y] [i] [j]; } } return temp; } } 
3
  • Can you give us a concise complete compile-able example of what you've tried? Commented Feb 25, 2015 at 13:58
  • Where does the 9 come from? Is it the number of elements in the matrix so that in next step it will be 27? Commented Feb 25, 2015 at 14:39
  • Yes I was doing that to test it Commented Feb 25, 2015 at 14:53

1 Answer 1

1

Ok this is pretty basic.

You have a square matrix A.

 [a b c] A = [e f g] [h i j] 

You copy the matrix 3 times to the right (horizontally) to get [A A A], and then you copy the strip 3 times to the bottom (vertically) to get a square strip of matrices

 [A A A] B = [A A A] [A A A] 

The new matrix is 3 times bigger per dimension, or totally 9 times bigger (per "area"). Now what you need to do is modify each submatrix -- add the corresponding element from the original matrix. You need to find the indices of the submatrix (the indices of the subsections), and use those same indices in the original matrix to get the corresponding scalar.

[9*A(0,0)+A 9*A(0,1)+A 9*A(0,2)+A] [9*A(1,0)+A 9*A(1,1)+A 9*A(1,2)+A] [9*A(1,0)+A 9*A(1,1)+A 9*A(1,2)+A] 

The last link we need to set is to know which element in the new matrix B corresponds to which section. B is 3 times bigger, e.g. B[0..8, 0..8] so if we divide the indices of element in B by 3 (integer division) we get it's section and this the indices of the scalar in A.

We say an element B(r,c) lays in section (i,j)=(r/3, c/3) and thus this element shall be modified with A(i,j). This last sentence pretty much sums it up.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.