Failed to check if a vector is inside a matrix

2

I'm 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 position of the matrix, I will jump to the next position of the vector to follow the check. My problem comes when I jump the row in the matrix and set the position of the vector to 0. I do not know how to establish 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; 
}
    
asked by AGeekM 13.09.2017 в 19:07
source

2 answers

1

According to the comment you have left in the other answer:

  

Develop, a method called buscaVectorEnMatriz() that has as parameters a matrix and a vector. The method performs the search of the vector in the matrix and returns the position of the first occurrence where there was a match. If the vector is not found in the matrix, the method returns null . The vector should only be searched by rows. Each row of the matrix can have a different length.

I must say that the solution is very simple, it is not necessary to compare each element of the vector, you just have to compare if the row corresponds to the same object.

What the function tries to do is to return the position where the vector is. Each row is an integer that means the memory position of said row , and can be compared with the integer of the vector entered in the function.

To return null , change void by Integer , and within for , I return the int converted to Integer .

Solution:

public Integer buscaVectorEnMatriz(int[][] matriz, int[] vector)
{
    for (int i=0;i<matriz.length;i++)
    {
        if(matriz[i]==vector){return new Integer(i);}
    }
    return null;
}

If what you want is to compare each element of the row, to know if it is the same row, first check that the length of the row is equal to the length of the vector entered. Otherwise, compare the elements, one by one.

public Integer buscaVectorEnMatriz(int[][] matriz, int[] vector)
{
    for (int i=0;i<matriz.length;i++)
    {
        if(matriz[i].length==vector.length)
        {
            boolean es_igual=true;
            for (int j=0;j<matriz[i].length;j++)
            {
                if(matriz[i][j]!=vector[j])
                {
                    es_igual=false;
                    break;
                }
            }
            if(es_igual)
            {
                return new Integer(i);
            }
        }
    }
    return null;
}
    
answered by 14.09.2017 / 17:04
source
4

Actually your problem is that the logic of your algorithm is wrong.

Your algorithm does not 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 does not work .. notice that the row 1/5/3 leaves posactual in 1 .. why ?? because it looks for the coincidence of the following way:

  • compare 3 with 1, give false, leave posactual at 0 (and here is already wrong, in reality you should not even keep looking at this row)
  • compare 3 with 5, it gives false, same case as above
  • compare 3 with 3, give it true, leave posactual in 1.

For it to be understood, it should not exist, since it should have the same value as j, since they are the same position to be compared.

That leaves you with another problem, like knowing if the whole row is exactly the same ... for that you can use a counter for every time you find a match, add one. and when finishing the row (when leaving the for(j) ) you could verify if that counter measures the same as the vector. and if they are the same, that row is the one that serves you. if not, you just put the counter to 0, and you continue with the row that follows.

As it is an exercise, I leave you the task of fixing it, but you know where the problems are.

    
answered by 13.09.2017 в 19:32