Results 1 to 20 of 20
Thread: Doubling size of 2d array
 07032011, 05:45 AM #1Member
 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!
 07032011, 05:50 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
Can you explain more what the output should be?
Should
Java Code:0,1,2,3 4,5,6,7 8,9,0,1
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
 07032011, 05:58 AM #3Member
 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
 07032011, 06:07 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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
Java Code:1,2,3,4
Java Code:1,2,3,4,1,2,3,4
 07032011, 06:13 AM #5Member
 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.
 07032011, 06:18 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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][];
 07032011, 07:10 AM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,361
 Blog Entries
 7
 Rep Power
 20
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,
Joscenosillicaphobia: the fear for an empty beer glass
 07052011, 10:33 PM #8Member
 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.
 07052011, 10:57 PM #9Member
 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!
 07052011, 11:06 PM #10Member
 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!
 07052011, 11:24 PM #11Member
 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?
 07052011, 11:31 PM #12
You forgot to explain what was wrong with the output and to show what you want the output to be.
 07052011, 11:36 PM #13Member
 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
 07052011, 11:43 PM #14Member
 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
 07052011, 11:43 PM #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?
 07062011, 12:33 AM #16Member
 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 0thcolumn values was different for values where j>0
 07062011, 01:03 AM #17
A comment on variable names: Is j for the rows or for the columns.
Why not use variable names: row and column
 07062011, 03:23 AM #18Member
 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
i'm about to go crazy!
 07062011, 03:28 AM #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.
 07062011, 05:19 AM #20Member
 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!
Similar Threads

Doubling size of 2d array
By osenna66 in forum New To JavaReplies: 0Last Post: 07032011, 04:30 AM 
size of array
By swathi dharmaraj in forum New To JavaReplies: 8Last Post: 04222011, 12:35 AM 
increase array size
By giorgi in forum New To JavaReplies: 45Last Post: 04082011, 04:56 PM 
Doubling the size of an array
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:42 PM 
Array size declaration
By JT4NK3D in forum New To JavaReplies: 3Last Post: 01182008, 10:37 PM
Bookmarks