Menu Close

Rotación de una matriz

En la siguiente figura, al rotar girando 90 grados en el sentido del reloj la matriz de la izquierda, obtenemos la de la derecha

   1 2 3        7 4 1
   4 5 6        8 5 2
   7 8 9        9 6 3

Definir la función

   rota :: Matrix Int -> Matrix Int

tal que (rota p) es la matriz obtenida girando en el sentido del reloj la matriz cuadrada p. Por ejemplo,

   λ> rota (fromList 3 3 [1..9])
   ( 7 4 1 )
   ( 8 5 2 )
   ( 9 6 3 )
 
   λ> rota (fromList 3 3 [7,4,1,8,5,2,9,6,3])
   ( 9 8 7 )
   ( 6 5 4 )
   ( 3 2 1 )

Soluciones

import Data.Matrix
 
rota :: Matrix Int -> Matrix Int
rota p = matrix n m (\(i,j) -> p ! (n+1-j,i))
    where m = nrows p
          n = ncols p
Medio

8 soluciones de “Rotación de una matriz

  1. Chema Cortés
    import Data.Matrix
    import Data.Vector as V (toList)
     
    rota1 :: Matrix Int -> Matrix Int
    rota1 p = fromLists [[ p!(i,j) | i <- reverse [1..a]] | j <- [1..b]]
        where a = nrows p
              b = ncols p
     
    rota2 :: Matrix Int -> Matrix Int
    rota2 p = fromLists . map reverse $ toCols p
        where toCols m = [V.toList (getCol n m) | n <- [1..ncols p]]
  2. erisan
    import Data.List as P
    import Data.Matrix as M
    import Data.Vector as V
     
     
    rota p = M.fromList m n (rota' p)
             where m = nrows p
                   n = ncols p
     
    rota' p = P.concat [P.reverse (V.toList (getCol x p)) | x <- [1..ncols p]]
  3. erisan
    import Data.List as P
    import Data.Matrix as M
    import Data.Vector as V
     
    rota :: Matrix Int -> Matrix Int
    rota p = M.fromList m n (rota' p)
             where m = nrows p
                   n = ncols p
     
    rota' :: Matrix a -> [a]
    rota' p = P.concat [P.reverse (V.toList (getCol x p)) | x <- [1..ncols p]]
  4. manvermor
    import Data.Matrix
     
    -- En colaboración con abrdelrod y pequeños ajustes finales
     
    rota :: Matrix Int -> Matrix Int
    rota p = fromList m n [ p!(m-j+1,i) | i <- [1..m], j <- [1..n]]
             where (m,n) = (nrows p,ncols p)
  5. manpende
    import Data.Matrix
     
    rota :: Matrix Int -> Matrix Int
    rota p = fromLists $ map reverse $ toLists $ transpose p
  6. fracruzam

    Version en Maxima hecha con la traducción a listas y la conversión a matriz

    rota (M) := block(
      [xs : rotando(matriz2Listas(M))],
      P : matrix(first(xs)),
      xs : rest(xs),
      for x in xs do (
        P : addrow(P,x)),
      P)$
     
    rotando (xss) := block(
      [xs : []],
      n : length (first (xss)),
      for j from 1 thru n do (
        xs : cons(map(first,xss),xs),
        xss : map(rest,xss)),
      xs)$
     
    matriz2Listas (M) := block(
      [xs : list_matrix_entries(M),
       ys : []],
      l : length(xs),
      n : sqrt(l),
      unless emptyp(xs) do (
        ys : cons(take_n_xs(n,xs),ys),
        xs : rest(xs,n)),
      ys)$
     
    take_n_xs (n,xs) := block(
      [ys : []],
      for j from 1 thru n do (
        ys : cons(first(xs),ys),
        xs : rest(xs)),
      ys)$
    • fracruzam

      La comprobación sería

      /*  (%i13) Q : matrix([1,2,3],[4,5,6],[7,8,9]);
                                        [ 1  2  3 ]
                                        [         ]
      (%o13)                            [ 4  5  6 ]
                                        [         ]
                                        [ 7  8  9 ]
      (%i14) rota(Q);
                                        [ 7  4  1 ]
                                        [         ]
      (%o14)                            [ 8  5  2 ]
                                        [         ]
                                        [ 9  6  3 ]
      (%i15) rota(%);
                                        [ 9  8  7 ]
                                        [         ]
      (%o15)                            [ 6  5  4 ]
                                        [         ]
                                        [ 3  2  1 ] */
  7. abrdelrod

    Definición alternativa a la de fracruzam en Maxima:

    nFilas (p) := first  (matrix_size (p))$
    nCols (p)  := second (matrix_size (p))$
     
    rotaA2 (p) :=
      genmatrix (lambda ([i,j], part (p, nFilas (p)-j+1, i)),
                 nCols (p),
                 nFilas(p))$

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.