perlmatrixpdl

Use Perl PDL to rotate a matrix


I would like to use Perl and PDL to achieve a rotation of a 3x3 matrix (if possible)

I.e original matrix

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

I would like to rotate, around 5, so it becomes new matrix

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

Effectively this is the same as How do you rotate a two dimensional array? but I'd like to use Perl and PDL.

Thanks for your help up front.


Solution

  • Perhaps not the most optimized way to do it:

    pdl> $m = sequence(3,3)+1
    pdl> p $m
    
    [
     [1 2 3]
     [4 5 6]
     [7 8 9]
    ]
    
    pdl> p $m->transpose->slice( ':', '-1:0' )
    
    [
     [3 6 9]
     [2 5 8]
     [1 4 7]
    ]