I have to work on a method called extract feature which has to check for the pattern i pass to the method in the matrix I pass
This is an example pattern = {{0,100,0},{255,255,255},{0,100,0}};
Though I pass any different pattern it has to check for that pattern in the matrix and make that matrix to zero where the pattern is found and return 1.
The following is the code that I have which is almost complete. But the problem is with the pattern matching which is kinda difficult to write.
Can any one give me some hints to complete the code.

public static int extractFeature(int[][] pattern, int[][] matrix) {
		int patternFound = 0;
		int rowp = pattern.length;
		int colump = pattern[0].length;
		for(int i = 0; i < 10; i++ ){
			for(int j = 0; j < 10; j++){
                          for(int l = 0; l<rowp; l++{
                            for(int k = 0; k < colump; k++){
                               if(matrix[i][j] == pattern[l][k]);
                               {
                                  ......
                               }
                            }
                          }
			}
		}			
		

		for(int i = 0; i < 10; i++ ){
			for(int j = 0; j < 10; j++){
				System.out.print(matrix[i][j] + "\t");
			}
			System.out.println("");
		}
		return patternFound;
	}

Thanks in advance. any help is appreciated

First of all, get the lengths of the "inner" and "outer" array of the pattern. Then, when looping through the matrix, there is no reason to go beyond matrix.outer.length - pattern.outer.length, or matrix.inner.length - pattern.inner.length as the pattern wouldn't fit in those spaces. Then loop until you find the first value from the pattern in the matrix. Then loop through the inner matrix comparing it's values to the "offset" values from the matrix. i.e.

pattern[outerIndex][innerIndex] == matrix[firstFoundIndex + patternOuterIndex][firstFoundIndex + patternInnerIndex]

.

Edited 6 Years Ago by masijade: n/a

thanks for the reply, appreciated. But I didnt really quite understand the outer length and inner length. Do you mean the length of the pattern ?
If so I have declared the row length with rowp and column length with columnp. Can you give me little boost on the method you just explained.

"outer" length and "inner" length are the lengths of the arrays in the 2D array. I.E. double[][] a = new double[5][6]; the "inner" length is 6 (i.e. the length of the "sub" arrays) and the "outer" length is 5 (i.e. the length of the encompassing array). Both the matrix and the pattern have "outer" and "inner" lengths.

Usually the "outer" will refer to the row and the "inner" to the column, but this is arbitrary, so long as any calculations to be done with the matrix are done with the proper "alignment", and the same "alignment" is used throughout the program.

I had modified the code in the following way can you please refer where I am going wrong

public static int extractFeature(int[][] pattern, int[][] matrix) {
		int patternFound = 0;
		int rowp = pattern.length;
		int colump = pattern[0].length;
		for(int i = 0; i < 10; i++ ){
			for(int j = 0; j < 10; j++){
			    for(int l = 0; l < rowp; l++){
				for(int k = 0; k < colump; k++){
				    if(pattern[l][k] == matrix[i+l][i+k]){
					patternFound = 1;
				    }
				}
			    }		
			}
		}

It throws me out of bound exception since it loops till the end of i is reached. Where do i put the break statement so that I exit out of the loop so that the whole i is not iterated. I am slightly confused. please help me

Edited 6 Years Ago by whimsical1987: n/a

Study this, and I really mean study this, as I hate doing this sort of thing, I don't think it usually helps, but I can see I will be talking for hours trying to nudge you in the right direction.

public class MatrixTest {
	private static int[][] matrix = {
			{ 1, 2, 3, 4, 5 },
			{ 2, 3, 4, 5, 6 },
			{ 3, 4, 5, 6, 7 },
			{ 4, 5, 6, 7, 8 },
			{ 5, 6, 7, 8, 9 }
		};

	private static int[][] pattern1 = {
			{ 2, 3 },
			{ 3, 4 }
		};

	private static int[][] pattern2 = {
			{ 4, 5, 6 },
			{ 5, 6, 7 }
		};

	private static int[][] pattern3 = {
			{ 2, 2 },
			{ 3, 3 }
		};

	private static int[] findStart(int[][] pattern) {
		int[] result = { -1, -1 };
		START:
		for (int i = 0; i <= matrix.length - pattern.length; i++) {
			for (int j = 0; j <= matrix[0].length - pattern[0].length; j++) {
				if (matrix[i][j] == pattern[0][0]) {
					if (confirmMatch(i, j, pattern)) {
						result[0] = i;
						result[1] = j;
						break START;
					}
				}
			}
		}
		return result;
	}

	private static boolean confirmMatch(int row, int col, int[][] pattern) {
		for (int k = 0; k < pattern.length; k++) {
			for (int l = 0; l < pattern[0].length; l++) {
				if (pattern[k][l] != matrix[row + k][col + l]) {
					return false;
				}
			}
		}
		return true;
	}

	public static void main(String[] args) {
		int[] vals = findStart(pattern1);
		if (vals[0] != -1) {
			System.out.println("Pattern 1 found at Row:  " + vals[0] + "  Col:  " + vals[1]);
		} else {
			System.out.println("Pattern 1 not found.");
		}

		vals = findStart(pattern2);
		if (vals[0] != -1) {
			System.out.println("Pattern 2 found at Row:  " + vals[0] + "  Col:  " + vals[1]);
		} else {
			System.out.println("Pattern 2 not found.");
		}

		vals = findStart(pattern3);
		if (vals[0] != -1) {
			System.out.println("Pattern 3 found at Row:  " + vals[0] + "  Col:  " + vals[1]);
		} else {
			System.out.println("Pattern 3 not found.");
		}
	}
}

Thank you very much. I really appreciate the patience and thanks for really helping me out.

This question has already been answered. Start a new discussion instead.