# Flipping a MATRIX in many ways (Directions) in MATLAB: Column flip, Row flip & Diagonal flip

Flipping a matrix? What would be the use of it?
Well the first use of it that comes to my mind is applicable on images. Sometimes we need to flip it, upside down, side ways, with the main diagonal or the other diagonal, since at the basic level image is a matrix we can apply the transformations of a matrix to an image also, but that depends on our application.

Take a randomly generated matrix 'x', such that it can be seen as,

>> x=rand(6,6)
x =
0.8147    0.2785    0.9572    0.7922    0.6787    0.7060
0.9058    0.5469    0.4854    0.9595    0.7577    0.0318
0.1270    0.9575    0.8003    0.6557    0.7431    0.2769
0.9134    0.9649    0.1419    0.0357    0.3922    0.0462
0.6324    0.1576    0.4218    0.8491    0.6555    0.0971
0.0975    0.9706    0.9157    0.9340    0.1712    0.8235

If we want to flip the matrix column wise, i.e, the first & the last column will be exchanged, we have to write the MATLAB command as:
>> x(:,end:-1:1) = x
Which will result in,
x =
0.7060    0.6787    0.7922    0.9572    0.2785    0.8147
0.0318    0.7577    0.9595    0.4854    0.5469    0.9058
0.2769    0.7431    0.6557    0.8003    0.9575    0.1270
0.0462    0.3922    0.0357    0.1419    0.9649    0.9134
0.0971    0.6555    0.8491    0.4218    0.1576    0.6324
0.8235    0.1712    0.9340    0.9157    0.9706    0.0975

Just Compare it from the original matrix, you will find that the first & the last column & all the corresponding columns are exchanged.

Again if we want to get back the original matrix, then we need to just enter the previous flipping command again,
>> x(:,end:-1:1) = xThe resultant matrix is back to original one.
x =
0.8147    0.2785    0.9572    0.7922    0.6787    0.7060
0.9058    0.5469    0.4854    0.9595    0.7577    0.0318
0.1270    0.9575    0.8003    0.6557    0.7431    0.2769
0.9134    0.9649    0.1419    0.0357    0.3922    0.0462
0.6324    0.1576    0.4218    0.8491    0.6555    0.0971
0.0975    0.9706    0.9157    0.9340    0.1712    0.8235

What if want to flip the matrix row wise?
The with a slight variation of the above command in which we have flipped the matrix column wise, will result in a flip row wise.

The command we need here is:
x(end:-1:1,:) = x
x =
compare the resultant matrix with the original one, you will find that the matrix is flipped row wise.
0.0975    0.9706    0.9157    0.9340    0.1712    0.8235
0.6324    0.1576    0.4218    0.8491    0.6555    0.0971
0.9134    0.9649    0.1419    0.0357    0.3922    0.0462
0.1270    0.9575    0.8003    0.6557    0.7431    0.2769
0.9058    0.5469    0.4854    0.9595    0.7577    0.0318
0.8147    0.2785    0.9572    0.7922    0.6787    0.7060

Again, in order to recover the matrix from the row flipped version we have to retype the command x(end:-1:1,:) = x.

NOTE: better use a temporary variable in order to preserve the original matrix.

Now its the turn of flipping the matrix through the matrix along with its 2 diagonals.

These example will work if we have a square matrix with odd number or rows/columns.

Flipping the matrix along its main diagonal.
The thing with flipping the matrix along with its main diagonal is, that its simply its transpose, for example we will generate a 5x5 random matrix & apply the flipping transformation through the transpose.

x=rand(5,5)
A 5x5 random matrix
x =
0.7572    0.0540    0.5688    0.7943    0.2630
0.7537    0.5308    0.4694    0.3112    0.6541
0.3804    0.7792    0.0119    0.5285    0.6892
0.5678    0.9340    0.3371    0.1656    0.7482
0.0759    0.1299    0.1622    0.6020    0.4505

>> x'  %flipping the matrix along with its main diagonal
ans =On comparing with the original matrix you will find this matrix flipped along its main diagonal.
0.7572    0.7537    0.3804    0.5678    0.0759
0.0540    0.5308    0.7792    0.9340    0.1299
0.5688    0.4694    0.0119    0.3371    0.1622
0.7943    0.3112    0.5285    0.1656    0.6020
0.2630    0.6541    0.6892    0.7482    0.4505

Flipping the matrix along its non main diagonal or the other diagonal.

Well thats a little bit tricky...., but here it is.... its a series of 2 commands

x=rand(5,5)
%generate a 5x5 random matrix for further operation
x =
0.0838    0.5383    0.9619    0.0844    0.9106
0.2290    0.9961    0.0046    0.3998    0.1818
0.9133    0.0782    0.7749    0.2599    0.2638
0.1524    0.4427    0.8173    0.8001    0.1455
0.8258    0.1067    0.8687    0.4314    0.1361

>> x=x'; %first generate the transpose
>> x(end:-1:1)=x %then flip it
%on comparing with the original matrix we find that following the above steps that the matrix is flipped along the other diaginal
x =
0.1361    0.1455    0.2638    0.1818    0.9106
0.4314    0.8001    0.2599    0.3998    0.0844
0.8687    0.8173    0.7749    0.0046    0.9619
0.1067    0.4427    0.0782    0.9961    0.5383
0.8258    0.1524    0.9133    0.2290    0.0838