2.9 Rotate a matrix by 90 degrees

Mathematica

mat = {{1, 2, 3}, 
       {4, 5, 6}, 
       {7, 8, 9}}; 
mat = Reverse[Transpose[mat]]
 

    {{3,6,9}, 
     {2,5,8}, 
     {1,4,7}}
 

 

Matlab

A=[1 2 3; 
   4 5 6; 
   7 8 9] 
A=rot90(A)
 

     3     6     9 
     2     5     8 
     1     4     7
 

 

Maple

A:=Matrix([[1,2,3],[4,5,6],[7,8,9]]); 
A:=ArrayTools:-FlipDimension(A,2);
 

[[3,2,1], 
 [6,5,4], 
 [9,8,7]]
 

A:=A^%T;
 

   [[3,6,9], 
    [2,5,8], 
    [1,4,7]]
 

 

Fortran

Using additional copy for the matrix

program f  implicit none 
  integer(4) ::i,j 
  integer(4), ALLOCATABLE ::A(:,:),B(:,:) 
 
  A = reshape ([1,2,3,   & 
                4,5,6,   & 
                7,8,9],  & 
               [3,3], & 
               order=[2,1] & 
              ) 
  B=A 
  i=0 
 
  do j=UBOUND(A, 2),LBOUND(A, 2),-1 
     i=i+1 
     B(i,:)=A(:,j) 
  end do 
 
  do i=LBOUND(B, 1),UBOUND(B, 1) 
       print *,B(i,:) 
  end do 
 
end program f
 
mp>gfortran -std=f2008 f.f90 
tmp>./a.out 
           3           6           9 
           2           5           8 
           1           4           7