It is strange that you managed to write that code without understanding it.
When you do:
out = in([4 8 1 ...]);
The first value of out is the 4th value of in, the 2nd value of out is the 8th of in, the 3rd value of out is the 1st of in, etc.
Since sOrder is a random permutation of all the integers from 1 to the number of elements in in ( r*c which you could simply obtain with numel(in)), out is just a random permutation of all the elements of in.