# Doubling size of 2d array

• 07-03-2011, 05:45 AM
osenna66
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!
• 07-03-2011, 05:50 AM
sunde887
Can you explain more what the output should be?

Should
Code:

```0,1,2,3 4,5,6,7 8,9,0,1```
Become
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.
• 07-03-2011, 05:58 AM
osenna66
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
• 07-03-2011, 06:07 AM
sunde887
Ah, I'm lazy and didn't read your whole question, sorry.

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

Code:

`1,2,3,4`
Into
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.
• 07-03-2011, 06:13 AM
osenna66
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.
• 07-03-2011, 06:18 AM
sunde887
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

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.
• 07-03-2011, 07:10 AM
JosAH
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
• 07-05-2011, 10:33 PM
osenna66
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.
• 07-05-2011, 10:57 PM
osenna66
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!
• 07-05-2011, 11:06 PM
osenna66
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!
• 07-05-2011, 11:24 PM
osenna66
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?
• 07-05-2011, 11:31 PM
Norm
You forgot to explain what was wrong with the output and to show what you want the output to be.
• 07-05-2011, 11:36 PM
osenna66
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
• 07-05-2011, 11:43 PM
osenna66
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
• 07-05-2011, 11:43 PM
Norm
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?
• 07-06-2011, 12:33 AM
osenna66
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
• 07-06-2011, 01:03 AM
Norm
A comment on variable names: Is j for the rows or for the columns.
Why not use variable names: row and column
• 07-06-2011, 03:23 AM
osenna66
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

i'm about to go crazy!
• 07-06-2011, 03:28 AM
Norm
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.
• 07-06-2011, 05:19 AM
osenna66
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!