MATLAB: How to generate n rows of random numbers such that each vector after the previous doesn’t repeat a value in the same column position

permutationrandom number generator

I want to generate 24 vectors of random numbers between 1 and 24 (inclusive), however I don't want to have any value in the same column position as the vector previously generated. For example:
If vector 1 is given by
v1 = [18 22 3 16 1 8 10 14 21 12 5 2 6 11 13 24 20 4 15 23 7 9 17 19];
vector 2 should be a different permutation such that 18 is not in the first column, 22 is not in the second column, 3 is not in the third column, etc.
Does anyone have an idea of how to approach this problem?

Best Answer

One approach:
v1 = randperm(24);
Out = [v1; circshift(v1, [0 randi(length(v1)-1)])];
It takes the first vector (first row) and circularly shifts it by a random number of positions to create the second row. So long as the shift argument to circshift is not 0 or equal to the length of the vector, the positions will never be the same in the two vectors.