# how to scramble positions of a matrix?

2 Ansichten (letzte 30 Tage)
Abirami am 6 Sep. 2014
Kommentiert: Roger Stafford am 7 Sep. 2014
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Roger Stafford am 6 Sep. 2014
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);
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Roger Stafford am 7 Sep. 2014
Every permutation always has an inverse. Writing "X2(X) = 1:4" causes X2 to be the inverse permutation of X. For example, if X = [3 2 4 1], then X2 will be [4 2 1 3] which is its inverse. That is, X2(X) = X(X2) = 1:4.
X X2
1 --> 3 --> 1
2 --> 2 --> 2
3 --> 4 --> 3
4 --> 1 --> 4
By being the two inverses of X and Y, that causes X2 and Y2 to do this:
A2 = B(X2,Y2) = A(X(X2),Y(Y2)) = A(X2(X),Y2(Y)) = A(1:4,1:4) = A
and you are back to A again.
Doing the previous X2 = zeros(1,4) is essential in creating an X2 of the correct size. It doesn't matter what it has in it, but if it is not created ahead of time, matlab will give an error. I could have written X2 = rand(1,4) instead and the result of X2(X) = 1:4 would still be the same.
Abirami am 7 Sep. 2014
Thank u very much sir....

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Andrei Bobrov am 7 Sep. 2014
Aout = B(X,Y)
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Roger Stafford am 7 Sep. 2014
That works only because the two permutations Abirami selected each happened to be their own inverse. Of the 24 possible permutations of 1:4 fourteen are not their own inverse. For example, if X is [4 1 3 2], then X(X) is [2 4 3 1], therefore X is not its own inverse, and B(X,Y) would not return A.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by