Advertisement
EstebanRojas

searchSubMatrix

Jun 7th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.25 KB | None | 0 0
  1. %----------------------------------------------------------------------------
  2. %  INTELIGENCIA ARTIFICIAL - 2024 - TP01
  3. %----------------------------------------------------------------------------
  4. % 8. Función 2. Escribir una función con las siguientes características:
  5. % a) Debe generar una matriz MG (dimensión 10x15) de números enteros aleatorios en el
  6. % rango [-10 , +10].
  7. % b) Debe encontrar todas las ocurrencia de una submatriz mc2x2 , dada por el usuario como
  8. % argumento de la función, dentro de la matriz MG.
  9. % c) La función devolverá la o las posiciones en un vector de nx2 (fila y columna) de todas las
  10. % ocurrencias de mc. En caso contrario generará un mensaje de error indicando que no hay
  11. % ocurrencias de mc.
  12. % d) La función deb verificar que la matriz ingresada sea de 2x2 y que su valores estén en el
  13. % rango indicado para MG. Caso contratio, se emitirá un mensaje de error .
  14. %----------------------------------------------------------------------------
  15. function findM = searchSubMatrix(SM)
  16.     MG=randi([-10,10],10,15)
  17. %     MG=[1 2 0 0 3 4 0 0; 1 2 0 0 3 4 0 0; 8 8 8 8 8 8 8 8; 9 9 0 0 9 0 0 9;9 9 0 0 9 0 0 9 ]
  18.     fil=0;
  19.     col=0;
  20.     ifil=1;
  21.     jcol=1;
  22.     bandReturn=true;
  23.     if validateSubMatrix(SM)
  24.         fprintf('ERROR.... \n')
  25.         fprintf('El error puede deberse a que la dimension es incorrecta \n')
  26.         fprintf('o que usted cargo con un valor fuera de rango [-10 10] \n')
  27.         return
  28.     end
  29.     [filMG,colMG]=size(MG);
  30.     [filSM,colSM]=size(SM);
  31.     for i=1:filMG-filSM+1
  32.         for j=1:colMG-colSM+1
  33.             if MG(i:(i+filSM-1),j:(j+colSM-1))==SM
  34.                 fil(ifil)=i;
  35.                 col(jcol)=j;
  36.                 ifil=ifil+1;
  37.                 jcol=jcol+1;
  38.                 bandReturn=false;
  39.             end
  40.         end
  41.     end
  42.     if bandReturn
  43.         fprintf('No hubo ocurrencias \n')
  44.         return
  45.     else
  46.         findM=[fil;col]';
  47.     end
  48. end
  49.  
  50. function band=validateSubMatrix(SM)
  51.     band=false;
  52.     [fil col]=size(SM);
  53.     if (fil~=2 && col ~=2)
  54.         band=true;
  55.     end
  56.     for i=1:2
  57.         for j=1:2
  58.             if(SM(i,j)<-10 || SM(i,j)>10)              
  59.                 band=true;
  60.                 return
  61.             end
  62.         end
  63.     end
  64. end
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement