I am doing an exercise in Java to check if a vector is inside a matrix. In case the first position of the vector coincides with the current one of the array, I will jump to the next position of the vector to continue the check. My problem comes when it comes to skipping rows in the matrix and setting the position of the vector to 0. I don't know how to set this condition.
public static PosicionMatriz buscaVectorEnMatriz(int[][] matriz, int[] vector) {
int coincidencia = 0;
int avance = 0;
PosicionMatriz pm;
int columna = 0;
int fila = 0;
vector = new int[3];
vector[0] = 3;
vector[1] = 5;
vector[2] = 6;
matriz = new int[3][3];
matriz[0][0] = 1;
matriz[0][1] = 5;
matriz[0][2] = 3;
matriz[1][0] = 3;
matriz[1][1] = 5;
matriz[1][2] = 6;
matriz[2][0] = 2;
matriz[2][1] = 9;
matriz[2][2] = 0;
for (int i = 0; i < matriz.length; i++) { // recorre el eje de la y
for (int j = 0; j < matriz[0].length; j++) {// recorre el eje de la x
for (int k = 0; k < vector.length; k++) { // recorre el vector
if (vector.length > matriz[i].length) { // Si longitud de vector > que longitud de la fila pasa a la siguiente
coincidencia = 0;
i++; // Saltamos de fila
}
if (vector[k] == matriz[i][j]) {
coincidencia++;
System.out.println("Se ha encontrado la coincidencia en " + "[" + (i) + "]" + "[" + (j) + "]");
} else {
k = 0; // Reestablecemos la posición del vector a la primera si no coincide con la de la matriz
coincidencia = 0;
}
if(coincidencia == vector.length) {
System.out.println("Finaliza la búsqueda");
}
}
}
}
return null;
}
Actually your problem is that the logic of your algorithm is wrong.
Your algorithm doesn't really know if a row of the matrix is equal to the vector, because it only searches positionally, and what happens in the first row of the matrix is the clear example of why it doesn't work... note that row 1/5 /3 leaves postcurrent at 1.. why?? because it looks for the match as follows:
For it to be understood, postcurrent should not even exist, since postcurrent should have the same value as j, since they are the same position to compare.
That still leaves you with another problem, like knowing if the whole row is exactly the same... for that you can use a counter for each time you find a match, adding one to it. and at the end of the row (when leaving the
for(j)
) you could check if that counter measures the same as the vector. and if they are equal, that row is the one that serves you. if not, you just set the counter to 0, and move on to the next row.As it is an exercise, I leave you the task of fixing it, but you already know where the problems are.
According to the comment you left on the other answer:
I must say that the solution is very simple, it is not necessary to compare each element of the vector, it is only necessary to compare if the row corresponds to the same object.
What the function tries to do is return the position where the vector is. Each row is an integer signifying the memory location of that row , and can be compared to the integer of the array entered into the function.
To return
null
, I changevoid
toInteger
, and inside thefor
, I return theint
converted toInteger
.Solution:
If what you want is to compare each element of the row, to know if it is the same row, you must first check that the length of the row is equal to the length of the entered vector. Otherwise, compare the items one by one.