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

• November 17th, 2011, 05:19 PM
Vibonacci
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.

greetings
• November 17th, 2011, 07:50 PM
Norm
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.
• November 17th, 2011, 08:17 PM
Vibonacci
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.
• November 17th, 2011, 08:32 PM
Norm
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?

Quote:

1 less row
What are the mappings for each element in the old array to each element in the new array?
• November 17th, 2011, 09:18 PM
Vibonacci
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.
• November 18th, 2011, 08:00 AM
Mr.777
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....
• November 18th, 2011, 08:49 AM
Norm
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.
• November 18th, 2011, 02:47 PM
Vibonacci
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.