0
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?

mayfly
  • 1
  • 2
  • It's possible you haven't gotten a response yet because the question is a bit unclear. For example, in your first code block, you define B twice, if you are trying to give the value B would become, you should write it outside the code or in a comment. As it is, its not clear what is code and what is explanation. – Teddy Ort Mar 23 '17 at 06:13
  • 1
    Apologies for inconvenience. I will present this query properly. – mayfly Mar 23 '17 at 06:23

1 Answers1

0

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.

Teddy Ort
  • 756
  • 6
  • 7
  • Thank you Sir, I got that dec(permute) = dec applies inverse of permute. – mayfly Mar 23 '17 at 16:28
  • Sir, you explained the general result. Is that the specific property defined in MATLAB? – mayfly Mar 23 '17 at 17:09
  • Yes, matlab syntax defines the meaning of `X(p)=z` as "place `z` into the p'th position in `X`" and the meaning of `z=Y(p)` as "take the p'th element in `Y` and place it in `z`" The two together give "place the p'th element from `Y` into the p'th position in `X` which, if `p` is a permutation vector equivalent to simpily setting `X=Y`. If you need more understanding see https://www.mathworks.com/help/matlab/matlab_oop/indexed-assignment.html – Teddy Ort Mar 23 '17 at 19:46