Results 1 to 20 of 20
  1. #1
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default 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. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #3
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    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. #5
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,530
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #9
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #10
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #11
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

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

  13. #13
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #14
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    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. #16
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

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

  18. #18
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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!

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    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. #20
    osenna66 is offline Member
    Join Date
    Jul 2011
    Posts
    18
    Rep Power
    0

    Default

    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

  1. Doubling size of 2d array
    By osenna66 in forum New To Java
    Replies: 0
    Last Post: 07-03-2011, 04:30 AM
  2. size of array
    By swathi dharmaraj in forum New To Java
    Replies: 8
    Last Post: 04-22-2011, 12:35 AM
  3. increase array size
    By giorgi in forum New To Java
    Replies: 45
    Last Post: 04-08-2011, 04:56 PM
  4. Doubling the size of an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM
  5. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 10:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •