问题
A = [1 2 3; 4 5 6; 7 8 9];
Now reshaping the matrix A to form a row vector gives B.
B = [1 4 7 2 5 8 3 6 9];
Evaluating polynomial function
f(x) = (7x+6x^2+3x^3)mod 9 by putting values for 'x' ranging from (1,...,9) since there are 9 elements.
Ex. For x=1, f(x) = 16 mod 9 = 7
For x=2, f(x) = 62 mod 9 = 8 till x = 9 results in permute.
permute = [7 8 3 1 2 6 4 5 9];
permute vector gives positions. Using matrix indexing, the positions of elements in row vector B are arranged according to permute vector resulting in enc.
enc = B(permute);
%enc = [3 6 7 1 4 8 2 5 9]
Thus, the original position of elements in A has been shuffled represented by new_A.
new_A = [3 1 2;6 4 5;7 8 9]; %reshaping new_A = (enc,3,3)
To get original matrix back, new_A is reshaped into a row vector 'dec' again.
dec = [3 6 7 1 4 8 2 5 9];
dec(permute) = dec;
dec = [1 4 7 2 5 8 3 6 9];
org_mat= reshape(dec,3,3)
org_mat = [1 2 3; 4 5 6; 7 8 9];
How does dec(permute)= dec works in this?
回答1:
The reason it works is because dec = enc = B(permute)
.
If you simply replace this into dec(permute) = dec;
you get dec(permute) = B(permute)
(only replace the second dec
.) This gives you dec=B
.
This is a general result. For example, for any permutation vector p
and vectors X
and Y
then if you set X(p)=Y(p)
then X=Y
because your placing the p'th element from Y
into the p'th element in X
thus exactly preserving Y
.
Finally, since dec=B
and you created B
using B=reshape(A,1,9)
then of course, reshape(dec,3,3)
gives you back A
as this is equivalent to reshape(B,3,3)
which just undoes the original reshape.
I hope this helps.
来源:https://stackoverflow.com/questions/42714860/i-have-a-a-3%c3%973-matrix-in-which-position-of-elements-are-to-be-shuffled-based-o