Reputation: 107
In a function I have as input two 2D Arrays(over 50x50), both with same Nr. of columns but different Nr. of Rows. Basically I need to remove the common columns containing digits from a range of 0 to 2
Example:
A: 3 0 0 0 0 1 0
5 0 0 6 0 0 2
2 0 0 7 1 0 0
B: 2 3 0 1 0 0 1
4 9 0 2 0 0 0
In this case I would have to remove column 3 , 6 and 7(to A and B), since Column 3 has only 0s, Column six only 0s and 1s, and The last Column all Nr. from 0 to 2.
I could use a function like LinearAlgebra.deleteColumns(A , 2 , 5,6); and LinearAlgebra.deleteColumns(B, 2, 5,6); but since my input Array is huge, I'd have to do a column parsing comparing both arrays.
Any ideas to approach this? Here's a raw idea of using 3 for loops(java)
for(int col=0; col < A[0].length; col++){ // step through each column
int cnt = 0;
for(int row = 0; row < B.length ; row ++){
if(!(B[col][row].equals(0 | 1 | 2))) {
cnt++;
}
}
if(cnt == 0) {
// store number of column -> use later on LinearAlgebra.deleteColumns
}
for(int row = 0; row < A.length ; row++){
if(!(A[col][row].equals(0 | 1 | 2)))
cnt++;
}
if(cnt == 0){
// check aswell all values of B otherwise iterate next column
}
}
Upvotes: 1
Views: 77
Reputation: 13713
boolean result[] = new boolean[<num of columns>];
for (int i = 0; i < result.length; ++i)
result[i] = true;
for (int row = 0; row < arrayA.length; ++row) {
for (int col = 0; col < arrayA[row].length; ++col)
result[col] &= arrayA[row][col] == 0;
}
for (int row = 0; row < arrayB.length; ++row) {
for (int col = 0; col < arrayB[row].length; ++col)
result[col] &= arrayB[row][col] == 0;
}
for (int i = 0; i < 6; ++i)
System.out.println(result[i]);
now each cell(column) in the result
array will indicate which column contains zeros for both arrays.
*Note: * This assums as you said that both arrays have the same number of columns
EDIT due to comment:
If you want to delete columns whose values are in a certain inclusive range use this condition :
result[col] &= arrayA[row][col] >= minRange && arrayA[row][col] <= maxRange;
for an exclusive range just remove the =
sign
and do the same for the second array
Upvotes: 1
Reputation: 8386
Helper function:
private boolean isColumnDeletable(final int column, final int[][] array) {
for (int row = 0; row < array.length; row++) {
if (array[row][column] != 0)
return false;
}
return true;
}
Example usage:
int[][] values1 = new int[][] {{1, 1, 1, 0, 1}, {2, 2, 2, 0, 2}, {3, 3, 3, 0, 3}};
int[][] values2 = new int[][] {{4, 4, 4, 0, 4}, {5, 5, 5, 0, 5}};
for (int column = 0; column < values1[0].length; column++) {
System.out.printf("Checking column %d: ", column + 1);
if (isColumnDeletable(column, values1) && isColumnDeletable(column, values2))
System.out.printf("Deletable!\n");
else
System.out.printf("Not deletable!\n");
}
Output:
Checking column 1: Not deletable!
Checking column 2: Not deletable!
Checking column 3: Not deletable!
Checking column 4: Deletable!
Checking column 5: Not deletable!
Upvotes: 1
Reputation: 3186
Make a boolean
flag, loop over the columns and decide which ones to remove.
Example to find deletable columns:
boolean deleteFlag;
for ( int i = 0; i < columnAmount; i++ ) {
deleteFlag = true;
for (int j = 0; j < firstTableRowAmount; j++ ) {
if (A[j][i] != 0) {
deleteFlag = false;
break;
}
}
if(!deleteFlag) {
continue;
}
for (int j = 0; j < secondTableRowAmount; j++ ) {
if (B[j][i] != 0) {
deleteFlag = false;
break;
}
}
if(deleteFlag) {
callDeleteColumnFunction(i);
}
}
and callDeleteColumnFunction(i)
is a function which deletes the i-th
column of both of the 2D arrays.
Upvotes: 1
Reputation: 2781
first of all you are maybe confusing your col and row order because you get the row count by using A[0].length
but you use it in B[col][row]
but for this part I let you see more in details.
anyways if I understand you problem well you are looking for the column that only contains 0.
For this you should first init a bool
at true and when a value different than 0 you put it at false. using the same process for the second with another bool
you finally compare the two bool to know if you have to delete the column ;)
Upvotes: 1