MATLAB: How to flip a general square matrix ( it could have even or odd number of rows/cols ) from it’s center in matlab

flippingMATLABmatrix

hi all
I have a general matrix , I want to flip the last half of it starting from it's center in matlab
To be clear ,I attach an image
I try the following
A=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25]
num_of_row=length(A(:,1))
if( mod(2,num_of_row)==0)
k=M/2;
else
k=(M-1)/2;
end
fliplr(A)
flip.jpeg

Best Answer

>> M = [1,2,3,4,5;2,6,7,9,10;3,7,11,12,13;4,9,12,16,20;8,10,13,20,25]
M =
1 2 3 4 5
2 6 7 9 10
3 7 11 12 13
4 9 12 16 20
8 10 13 20 25
>> X = [5,4,1,2,3];
>> Z = M(X(end:-1:1),X)
Z =
13 12 3 7 11
10 9 2 6 7
5 4 1 2 3
20 16 4 9 12
25 20 8 10 13