{"id":9476,"date":"2016-02-02T17:17:37","date_gmt":"2016-02-02T22:17:37","guid":{"rendered":"http:\/\/blog.espol.edu.ec\/ccpg1001\/?p=9476"},"modified":"2026-08-18T10:11:28","modified_gmt":"2026-08-18T15:11:28","slug":"s3eva2015tii_t3-funciones-matrices-rotar-extraer","status":"publish","type":"post","link":"https:\/\/blog.espol.edu.ec\/algoritmos101\/fp-s3eva20\/s3eva2015tii_t3-funciones-matrices-rotar-extraer\/","title":{"rendered":"s3Eva2015TII_T3 funciones matrices rotar, extraer"},"content":{"rendered":"\n<p><em><strong>Ejercicio<\/strong><\/em>: <a href=\"https:\/\/blog.espol.edu.ec\/algoritmos101\/fp-3eva20\/3eva2015tii_t3-funciones-matrices-rotar-extraer\/\" data-type=\"post\" data-id=\"2691\">3Eva2015TII_T3 funciones matrices rotar, extraer<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image alignright size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"394\" height=\"221\" src=\"http:\/\/blog.espol.edu.ec\/algoritmos101\/files\/2015\/03\/rotarmatriz.gif\" alt=\"rotar matriz\" class=\"wp-image-1756\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">literal a<\/h2>\n\n\n\n<p>Si se usa la matriz del ejemplo, se convierte en arreglo. Luego se crea una matriz de las mismas dimensiones (n,m) intercambiadas (m,n).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nmatriz = &#x5B;&#x5B;0,4,0],\n          &#x5B;5,6,7]]\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 3; title: ; notranslate\" title=\"\">\nmatriz = np.array(matriz)\n\nn,m = matriz.shape\ntabla = np.zeros(shape=(m,n),dtype=int)\n<\/pre><\/div>\n\n\n<p>La rotaci\u00f3n inicia copiando cada valor de casilla i,j en la posici\u00f3n f,c determinada por:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 19; title: ; notranslate\" title=\"\">\nf = j\nc = (n-1)-i\ntabla&#x5B;f,c] = matriz&#x5B;i,j]\n<\/pre><\/div>\n\n\n<p>Por lo que se usan todas las combinaciones de fila,columna de la matriz dada usando bucles para recorrer la matriz.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 15; title: ; notranslate\" title=\"\">\n    i = 0   # inicia rotaci\u00f3n\n    while not(i&gt;=n):\n        j = 0\n        while not(j&gt;=m):\n            f = j\n            c = (n-1)-i\n            tabla&#x5B;f,c] = matriz&#x5B;i,j]\n            j = j + 1\n        i = i + 1\n<\/pre><\/div>\n\n\n<p>Luego se realizan tantas rotaciones seg\u00fan el valor dado en k usando otro bucle que cuenta las rotaciones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">literal b<\/h2>\n\n\n\n<p>De forma semejante al literal anterior, la matriz recibida se convierte a un arreglo. Para el resultado se crea otra matriz de tama\u00f1o (t,t)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; first-line: 31; title: ; notranslate\" title=\"\">\nmatriz = np.array(matriz)\nn,m = matriz.shape\nesquina = np.zeros(shape=(t,t), dtype=int)\n<\/pre><\/div>\n\n\n<p>Para luego determinar las coordenadas <code>f,c<\/code> de la esquina requerida mediante <code>k<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; first-line: 38; title: ; notranslate\" title=\"\">\nif (k==0): # superior izquierda\n    f = i\n    c = j\nif (k==1): # superior derecha\n    f = i\n    c = (m-t)+j\nif (k==2): # inferior derecha\n    f = (n-t)+i\n    c = (m-t)+j\nif (k==3): # inferior izquierda\n    f = (n-t)+i\n    c = j\n<\/pre><\/div>\n\n\n<p>La copia de las casillas se limita por la variable <code>t<\/code>.<\/p>\n\n\n\n<p>Ejemplo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>matriz = &#091;&#091;0,4,0],\n          &#091;5,6,7]]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;<strong>extraeresquina<\/strong>(matriz,1,2)<br>&#091;&#091;4 0]<br> &#091;6 7]]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Algoritmo en Python<\/h2>\n\n\n\n<p><em><strong>Tarea<\/strong><\/em>: Integrar con tema 4<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code alignwide\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# 3Eva2015TII_T3 funciones matrices rotar, extraer\n# version con lazos y condicionales. \n# tarea: simplificar lazos con 'for'\nimport numpy as np\n\n# literal a\ndef rotando(matriz,k):\n    matriz = np.array(matriz)\n    \n    cuenta = 0  #cuenta rotaciones\n    while not(cuenta&gt;=k):\n        n,m = matriz.shape\n        tabla = np.zeros(shape=(m,n),dtype=int)\n        \n        i = 0   # inicia rotaci\u00f3n\n        while not(i&gt;=n):\n            j = 0\n            while not(j&gt;=m):\n                f = j\n                c = (n-1)-i\n                tabla&#x5B;f,c] = matriz&#x5B;i,j]\n                j = j + 1\n            i = i + 1\n        \n        matriz = np.copy(tabla)\n        cuenta = cuenta + 1\n    return(matriz)\n\n# literal b\ndef extraeresquina(matriz,k,t):\n    matriz = np.array(matriz)\n    n,m = matriz.shape\n    esquina = np.zeros(shape=(t,t), dtype=int)\n    i = 0\n    while not(i&gt;=t):\n        j = 0\n        while not(j&gt;=t):\n            if (k==0): # superior izquierda\n                f = i\n                c = j\n            if (k==1): # superior derecha\n                f = i\n                c = (m-t)+j\n            if (k==2): # inferior derecha\n                f = (n-t)+i\n                c = (m-t)+j\n            if (k==3): # inferior izquierda\n                f = (n-t)+i\n                c = j\n            esquina&#x5B;i,j]=matriz&#x5B;f,c]\n            j = j+1\n        i = i+1   \n    return(esquina)\n\n# PROGRAMA PRUEBA -----\nmatriz = &#x5B;&#x5B;0,4,0],\n          &#x5B;5,6,7]]\nprint(np.array(matriz))\nprint('rotando:')\nprint(rotando(matriz,3))\nprint('esquina')\nprint(extraeresquina(matriz,1,2))\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt;\n&#091;&#091;0 4 0]\n &#091;5 6 7]]\nrotando:\n&#091;&#091;0 7]\n &#091;4 6]\n &#091;0 5]]\nesquina\n&#091;&#091;4 0]\n &#091;6 7]]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ejercicio: 3Eva2015TII_T3 funciones matrices rotar, extraer literal a Si se usa la matriz del ejemplo, se convierte en arreglo. Luego se crea una matriz de las mismas dimensiones (n,m) intercambiadas (m,n). La rotaci\u00f3n inicia copiando cada valor de casilla i,j en la posici\u00f3n f,c determinada por: Por lo que se usan todas las combinaciones de [&hellip;]<\/p>\n","protected":false},"author":8043,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"wp-custom-template-entrada-fp-ejemplos","format":"standard","meta":{"footnotes":""},"categories":[143],"tags":[58,157],"class_list":["post-9476","post","type-post","status-publish","format-standard","hentry","category-fp-s3eva20","tag-ejemplos-python","tag-fundamentos-programacion"],"_links":{"self":[{"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/posts\/9476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/users\/8043"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/comments?post=9476"}],"version-history":[{"count":7,"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/posts\/9476\/revisions"}],"predecessor-version":[{"id":25464,"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/posts\/9476\/revisions\/25464"}],"wp:attachment":[{"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/media?parent=9476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/categories?post=9476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/algoritmos101\/wp-json\/wp\/v2\/tags?post=9476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}