# Thread: Doubling size of 2d array

1. Member
Join Date
Jul 2011
Posts
18
Rep Power
0

## Doubling size of 2d array

This post concerns doubling the size of a matrix. The method takes an int[][] as imput and outputs a new int[][] double in size.

Some of the code I have managed to write:

public static int[][] doubleSize(int[][] m) {
int[][] doubleSizeArray = new int[2 * m.length][2 * m[0].length];
for (int i=0, i < m.length; i++) {
for (int j=0; j < m[0].length; j++) {
doubleSizeArray[i][j] = m[i][j];

//i am at a loss as to what to do next

}
} //return statement
}

In the assignment question it is written: "every single value in the original matrix should be duplicated 3 additional times so that it is present 4 times in the new matrix". I don't know how to implement this in the java language. Any help would be greatly appreciated!

2. Can you explain more what the output should be?

Should
Java Code:
```0,1,2,3
4,5,6,7
8,9,0,1```
Become
Java Code:
```0,1,2,3,0,1,2,3
4,5,6,7,4,5,6,7
8,9,0,1,8,9,0,1```
Or should it be different? It may be helpful to start small and double the size of a single dimension array. A 2d array is simply an array where each element is an array.

3. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
There are no further specifications on the output, but ima assume it should be something like this:

original:

1, 2, 3
4, 5, 6

becomes:

1, 2, 3, 1, 2, 3
4, 5, 6, 4, 5, 6
1, 2, 3, 1, 2, 3
4, 5, 6, 4, 5, 6

You want to basically take an array that is 3x3 and make it an array that is 6x6, thefirst thing you should really do is worry about just creating that array, but empty. This array is basically an array where each of the 6 elements is an array that is 6 elements long. It would probably be helpful to write a method which simple doubles an array, and array of size 4 becomes an array of size 8(this can be done in a nested loop, but a helper method may make it easier to visualize.

Make a method that turns

Java Code:
`1,2,3,4`
Into
Java Code:
`1,2,3,4,1,2,3,4`
This step may give you an idea of how to do the entire problem, if not, post the completed method and what your try on the full problem looks like.

5. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
ok thanks for the help!

I'll try working on it some more...but only tomorrow...i am also lazy and very tired at the moment. ugh.

6. Alright, good luck, it's surprisingly easy if you create that helper method, you will have to use the % operator, but the logic should be fairly straightforward. The double sized array should look something like this

Java Code:
`int [][] doubled = new int[old.length * 2][];`
You should be able to double the second part but it's not entirely necessary, you will be indexing into the first part of this array and inserting new arrays.

7. Apply a bit of math; let M be the original matrix, width w and height h, and let N be a matrix twice the size of M (2*w, 2*h). For element M[r][c] you find four elements in N: N[r][c], N[r][w+c], N[h+r][c] and N[h+r][w+c]. You can do that with a single nested loop.

kind regards,

Jos

8. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
Ok so turns out that if the input is:

1, 2, 3
4, 5, 6

The output should be:

1, 1, 2, 2, 3, 3
1, 1, 2, 2, 3, 3
4, 4, 5, 5, 6, 6
4, 4, 5, 5, 6, 6

This method is going to be applied to an image.

9. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
this is the code i've come up with:

public static int[][] doubleSize(int[][] m) {
int[][] doubleSizeArray = new int[2 * m.length][2 * m[0].length];
for (int i=0; i < m.length; i++) {
for (int j=0; j < m[0].length; j++) {
doubleSizeArray[i][j] = m[i][j];
doubleSizeArray[i][j+1] = m[i][j];
doubleSizeArray[i+1][j] = m[i][j];
doubleSizeArray[i+1][j+1] = m[i][j];
}
}
return doubleSizeArray;
}

...but the ouput is not what it should be.
I don't know what i am doing wrong!

10. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
i realize now that for any values not in the 0th column of the original matrix, i would need to write different code.
i suppose i could use an if statement type thing...but how can i write if(index is not equal to any index where column=0)???

help plz!

11. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
i worked on it some more and now this is the code that i have:

public static int[][] doubleSize(int[][] m) {
int[][] doubleSizeArray = new int[2 * m.length][2 * m[0].length];
for (int i=0; i < m.length; i++) {
for (int j=0; j < m[0].length; j++) {
if (j==0) {
doubleSizeArray[i][j] = m[i][j];
doubleSizeArray[i][j+1] = m[i][j];
doubleSizeArray[i+1][j] = m[i][j];
doubleSizeArray[i+1][j+1] = m[i][j];
}
else if (j > 0) {
doubleSizeArray[i][j+1] = m[i][j];
doubleSizeArray[i][j+2] = m[i][j];
doubleSizeArray[i+1][j+1] = m[i][j];
doubleSizeArray[i+1][j+2] = m[i][j];
}
}
}

return doubleSizeArray;
}

with this code, if input is:
1, 2
3, 4

then output ends up being this:

1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4
0, 0, 0, 0

what am i doing wrong?

12. You forgot to explain what was wrong with the output and to show what you want the output to be.

13. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
right, so the output i get from the code i wrote is:

1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4
0, 0, 0, 0

and this is what i want the output to be:

1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4

14. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
like i said earlier, i need to be able to apply this method to doubling the size of an image...so each value of the matrix is representative of an image pixel

15. Have you written down a mapping to show how to copy old to new?
0,0 => 0,0 & 0,1 & 1,0 & 1,1
etc for all 4 locations in the old array. Then look at the pattern and figure out how to write the loops

What is the reason for the if tests on the value of j?

16. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
i wrote the if statements on the value of j because i thought the pattern for 0th-column values was different for values where j>0

17. A comment on variable names: Is j for the rows or for the columns.
Why not use variable names: row and column

18. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
once again, i've reworked the code but the output is still wrong.

this is the new code i've written:
public static int[][] doubleSize(int[][] m) {
int[][] doubleSizeArray = new int[2 * m.length][2 * m[0].length];
for (int i=0; i < m.length; i++) {
for (int j=0; j < m[0].length; j++) {
doubleSizeArray[i+i][j+j] = m[i][j];
doubleSizeArray[i+i][j+j+1] = m[i][j];
doubleSizeArray[i+2][j+j] = m[i][j];
doubleSizeArray[i+2][j+j+1] = m[i][j];
}
}

return doubleSizeArray;
}

this the is input:
1, 2
3, 4

this is the output i'm getting:
1, 1, 2, 2
0, 0, 0, 0
3, 3, 4, 4
3, 3, 4, 4

and this is the output WANT:
1, 1, 2, 2
1, 1, 2, 2
3, 3, 4, 4
3, 3, 4, 4

19. Keep going.
For your 2x2 conversion to 4x4 did you write out all that mappings from source to target?
And no pattern came out that you could generalize on to find the loops needed?
Maybe you need another way to look at it.

20. Member
Join Date
Jul 2011
Posts
18
Rep Power
0
ugh, finally got it! here it is!

public static int[][] doubleSize(int[][] m) {
int[][] doubleSizeArray = new int[2 * m.length][2 * m[0].length];
for (int i=0; i < m.length; i++) {
for (int j=0; j < m[0].length; j++) {
for (int k=i+i; k <= i+2; k++) {
for (int l=j+j; l <= j+j+1; l++) {
doubleSizeArray[k][l] = m[i][j];
}
}

}
}

return doubleSizeArray;
}

thanks for all the help!