MATLAB: How to create a matrix with a randomized permutation of integers

permutationssliding puzzle

Alternatively: What is the matrix equivalent of randperm(n,k)?
I can't figure out a simple way to do this.

Best Answer

Can you accomplish what you want by simply reshaping a random vector into a matrix?
reshape(randperm(16,16),[4,4])
ans =
4 12 11 15
7 5 13 9
14 8 2 1
6 10 3 16