Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %----------------------------------------------------------------------------
- % INTELIGENCIA ARTIFICIAL - 2024 - TP01
- %----------------------------------------------------------------------------
- % 8. Función 2. Escribir una función con las siguientes características:
- % a) Debe generar una matriz MG (dimensión 10x15) de números enteros aleatorios en el
- % rango [-10 , +10].
- % b) Debe encontrar todas las ocurrencia de una submatriz mc2x2 , dada por el usuario como
- % argumento de la función, dentro de la matriz MG.
- % c) La función devolverá la o las posiciones en un vector de nx2 (fila y columna) de todas las
- % ocurrencias de mc. En caso contrario generará un mensaje de error indicando que no hay
- % ocurrencias de mc.
- % d) La función deb verificar que la matriz ingresada sea de 2x2 y que su valores estén en el
- % rango indicado para MG. Caso contratio, se emitirá un mensaje de error .
- %----------------------------------------------------------------------------
- function findM = searchSubMatrix(SM)
- MG=randi([-10,10],10,15)
- % 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 ]
- fil=0;
- col=0;
- ifil=1;
- jcol=1;
- bandReturn=true;
- if validateSubMatrix(SM)
- fprintf('ERROR.... \n')
- fprintf('El error puede deberse a que la dimension es incorrecta \n')
- fprintf('o que usted cargo con un valor fuera de rango [-10 10] \n')
- return
- end
- [filMG,colMG]=size(MG);
- [filSM,colSM]=size(SM);
- for i=1:filMG-filSM+1
- for j=1:colMG-colSM+1
- if MG(i:(i+filSM-1),j:(j+colSM-1))==SM
- fil(ifil)=i;
- col(jcol)=j;
- ifil=ifil+1;
- jcol=jcol+1;
- bandReturn=false;
- end
- end
- end
- if bandReturn
- fprintf('No hubo ocurrencias \n')
- return
- else
- findM=[fil;col]';
- end
- end
- function band=validateSubMatrix(SM)
- band=false;
- [fil col]=size(SM);
- if (fil~=2 && col ~=2)
- band=true;
- end
- for i=1:2
- for j=1:2
- if(SM(i,j)<-10 || SM(i,j)>10)
- band=true;
- return
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement