MATLAB: How to scramble positions of a matrix

matrix arraymatrix manipulationpermutescramble

Hello, I've been trying to scramble the positions of a matrix. I have generated two sequences which help us to scramble the positions of the matrix. Eg;
X=[1 3 2 4]
Y=[3 4 1 2]
ie (X,Y) is used to scramble the positions of a 4x4 matrix
Let A= 56 77 228 99
88 31 52 21
32 74 90 28
66 99 42 33
now using the above two vectors i have the following order
B= (1,3) (1,4) (1,1) (1,2)
(3,3) (3,4) (3,1) (3,2)
(2,3) (2,4) (2,1) (2,2)
(4,3) (4,4) (4,1) (4,2)
ie
B= 228 99 56 77
90 28 32 74
52 21 88 31
42 33 66 99.
in addition i would also like to know how i can reverse the positions again ie th first element should be placed at (1,3). please help thanks in advance

Best Answer

Just use the inverses of X and Y:
X2 = zeros(1,4); X2(X) = 1:4;
Y2 = zeros(1,4); Y2(Y) = 1:4;
A2 = B(X2,Y2);