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?
The reason it works is because
dec = enc = B(permute)
.If you simply replace this into
dec(permute) = dec;
you getdec(permute) = B(permute)
(only replace the seconddec
.) This gives youdec=B
.This is a general result. For example, for any permutation vector
p
and vectorsX
andY
then if you setX(p)=Y(p)
thenX=Y
because your placing the p'th element fromY
into the p'th element inX
thus exactly preservingY
.Finally, since
dec=B
and you createdB
usingB=reshape(A,1,9)
then of course,reshape(dec,3,3)
gives you backA
as this is equivalent toreshape(B,3,3)
which just undoes the original reshape.I hope this helps.