# reverse a matrix

We can count backwards to flip a matrix A upside down with

sage: A[::-1,:]=A; A


Can we do it from left to right; I mean, for the matrix A=

[ 3  2 -5  0]
[ 1 -1  1 -4]
[ 1  0  1 -3]


Is there an easy way to obtain the following matrix

[ 0 -5  2  3]
[-4  1 -1  1]
[ 3  1  0  1]

edit retag close merge delete

Sort by » oldest newest most voted

You're almost there!

sage: a = matrix(3, 3, range(9))
sage: b = a[::-1,:]      # reverse lines
sage: c = a[:,::-1]      # reverse columns
sage: d = a[::-1,::-1]   # reverse both
sage: a, b, c, d
(
[0 1 2]  [6 7 8]  [2 1 0]  [8 7 6]
[3 4 5]  [3 4 5]  [5 4 3]  [5 4 3]
[6 7 8], [0 1 2], [8 7 6], [2 1 0]
)

more

Well, you could use the matrix_from_columns method:

sage: A = matrix(3, 3, range(9))
sage: A
[0 1 2]
[3 4 5]
[6 7 8]
sage: A.matrix_from_columns(range(2,-1,-1))
[2 1 0]
[5 4 3]
[8 7 6]


Or you could take the transpose, use your idea to flip upside down, and then take another transpose:

sage: A.transpose()[::-1,:].transpose()
[2 1 0]
[5 4 3]
[8 7 6]

more