Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: produce new matrix from old one with certain elements filtered, very weird problem

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default produce new matrix from old one with certain elements filtered, very weird problem

    Hello fellow Java programmers,

    I need a function that filters certain elements from a String matrix and returns a new string matrix. The new matrix is 1 row shorter because I want to filter every certain occurrence of a string from the whole matrix. The spots that are filtered will be filled up with the elements from below. Elements may not be reused.
    Important feature of the original matrix is that the elements in every column cannot occur more then once (but can occur multiple times within a row). You could not be sure everything would fit in a new matrix with a row less if this was not the case.

    Example 1 =

    A B A
    B C B
    C A C
    'A' will be filtered and new matrix SHOULD BE:
    B B B
    C C C

    same example but 'B' will be filtered; new matrix SHOULD BE:
    A C A
    C A C


    Example 2 =
    B B E B E A A F C A E
    C F B F F E D D A E C
    E A F D B F B B E B B
    F C A C D B E C F D A
    A E C E A D F A B C F
    D D D A C C C E D F D

    'D' will be filtered; new matrix SHOULD BE:
    B B E B E A A F C A E
    C F B F F E B B A E C
    E A F C B F E C E B B
    F C A E A B F A F C A
    A E C A C C C E B F F

    I hope the examples are more clear then my explanation. The problem for me is that as soon a I find a element to filter, all the other elements of that column need to be 1 row higher. But I am using elements multiple times.
    My faulty code:


    String[][] filterMatrix (char charToBeFiltered, String[][] oldMatrix)
    {
    String[][] newMatrix = new String[oldMatrix.length - 1][oldMatrix[0].length];

    for (int i = 0; i < newMatrix.length; i++)
    {
    for (int j = 0; j < newMatrix[0].length; j++)
    {
    if (oldMatrix[i][j].charAt(0) == charToBeFiltered)
    {
    newMatrix[i][j] = oldMatrix[i+1][j];
    }
    else
    {
    newMatrix[i][j] = oldMatrix[i][j];
    }
    }
    }
    return newMatrix;
    }

    for example 1 output is (if 'A' is to be filtered):
    B B B
    B C B

    problem: the second row is a copy of the original second row but the first row is filtered correctly. The B's are used multiple times and that is not allowed. Every element can only be used once.

    for example 2 output is (if 'D' is to be filtered):
    B B E B E A A F C A E
    C F B F F E B B A E C
    E A F C B F B B E B B
    F C A C A B E C F C A
    A E C E A C F A B C F

    problem: row 3 column 7 and 8.. the two B's are used multiple times. The first 2 rows are filtered correctly but from there it goes wrong.

    The overall problem is the nested for loops and the code in it. Very frustrating as I'm thinking about the problem for hours now and can't seem to get any closer to a solution. I hope somebody with more experience can provide some help.

    Thanks in advance,

    greetings


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    What are the rules for creating the new array? Your first example shows a 3x3 array being converted to a 2x3 array. Why not a 3X2 array?
    Can there be partially filled rows or columns?

    For these kinds of problems you need to use paper and pencil and write down how the all the elements in a 3x3 are moved to the 2x3 array.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Vibonacci (November 17th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    Condition of the original matrix:
    any element can only occur once in column:
    A B
    A B

    is not allowed

    but
    A B
    B A

    is allowed!


    The conditions of the new matrix are:

    -1 less row then the original matrix => [3][3] becomes [2][3], [6][11] becomes [5][11]
    -Every other element besides the instances of the filtered element have to be represented in the new matrix
    -Every element can only be used once (this applies to the non filtered elements).
    -Every element below a filtered element (column + 1) has to be moved up but cannot be used more then once.

    Given the condition of the original matrix it should be always possible (probably possible to prove mathematically) to create a new matrix with 1 less row.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    Given this 3X3 array:
    A B C
    B C B
    C A C

    What would you get if you remove A here?

    1 less row
    What are the mappings for each element in the old array to each element in the new array?

  6. The Following User Says Thank You to Norm For This Useful Post:

    Vibonacci (November 17th, 2011)

  7. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    Given this 3X3 array:
    A B C
    B C B
    C A C




    That matrix is not allowed as input since 'C' occurs two times in the third column.

    A B C
    B C B
    C A A
    would be allowed
    no duplicate elements are allowed in a column, only in a row.

    output should be (filter a)
    B B C
    C C B

    or filter (b)

    A C C
    C A A



    or new matrix
    A B C D
    D C B A
    C A A B
    B D D C

    output filter (a):
    D B C D
    C C B B
    B D D C

    output filter (b):
    A C C D
    D A A A
    C D D C

    etc.. When looking at the examples it is easy to get a feel of a certain structure but its really hard to formulate into code.

  8. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    So, what do you want to do now? Talking about your last post....

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    Yes its a weird problem. You need to have an algorithm to solve your problem.
    If you have that, then you write the code to implement it. If you have problems coding some part of the algorithm, post it with your questions.

  10. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: produce new matrix from old one with certain elements filtered, very weird proble

    After a good night sleep I was filled with creativity... blablabla

    The solution: First make a new matrix that is copy of the matrix in the parameter and a matrix that will be the correct output.
    The point is to alter the old matrix if you find a element that has to be filtered.. if you find a illegal element you take a element below you and move all elements below you (in that column) 1 spot up. The last row will contain garbage but that is not a problem if you just ignore that information. The problem with my original code was that I did not move all elements below a filtered element up one spot.. only at the first occurrence of a illegal element.




    String[][] filterMatrix (char charTobeFiltered, String[][] oldMatrix)
    {
    String[][] oldMatrixCopy = new String[oldMatrix.length][oldMatrix[0].length];
    oldMatrixCopy = oldMatrix;
    String[][] newMatrix = new String[oldMatrix.length - 1][oldMatrix[0].length];

    for (int i = 0; i < newMatrix.length; i++)
    {
    for (int j = 0; j < newMatrix[0].length; j++)
    {
    if (oldMatrixCopy[i][j].charAt(0) == charTobeFiltered)
    {
    for (int k =i; k < newMatrix.length; k++)
    {
    oldMatrixCopy[k][j] = oldMatrixCopy[k+1][j];
    }
    }
    newMatrix[i][j] = oldMatrixCopy[i][j];
    }
    }
    return newMatrix;
    }


    frustrating but oh so liberating when it finally succeeds.. thx and hope somebody can find some use in this.

Similar Threads

  1. I'm having a weird problem with my browsers
    By javapenguin in forum Computer Support
    Replies: 11
    Last Post: August 18th, 2011, 03:41 AM
  2. Weird ActioListener problem
    By macko in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 12th, 2011, 01:55 AM
  3. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  4. Inverse of a Matrix problem
    By shivamchauhan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 17th, 2011, 01:40 PM
  5. Jsp weird problem
    By johniem in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 5th, 2010, 06:46 AM