MATLAB: How to generate an integer matrix without any replacement of columns that contain the same numbers regardless of order

random num

Hello. I have a problem to solve in matlab:
I want to generate a 5* 100 integer matrix (5 rows, 100 columns). For each column, I need to randomly return 5 numbers without replacement from 1 to 24. I found matlab has a random function to do this:
y = randsample(n,k) : returns k observations sampled uniformly at random, without replacement, from the data in data. (e.g., y = randsample (5, 24) returns [1 3 15 17 20] from the integer set 1:24)
Here is the tricky thing. I want to generate a matrix with 100 columns that doesn't contain any two columns that have the same numbers regardless of order.
For example,
[ 1 3 5 7 9]' [ 3 1 7 9 5]'
Even the orders are different in the arrays above, they are considered having the same numbers, thus should not both appear in the matrix.
I figured out a method to do this, but it may not be efficient:
1. Create an empty 5*100 matrix A; 2. Use randsample(5,24) to return an array with 5 numbers. Sort the numbers from smallest to largest, and store them in the first column of A. 3. Use randsample(5,24) again and sort the output array. Then compare the array with the first column: if they're different, then store it as the 2nd column, else, use ransample to generate a new array and compare… 4. Repeat the following step until the matrix is filled up: Use randsample to create a new column and then compare it with old columns in the matrix. If it's different, store it as a new column.
I wonder if there is any easier method in matlab to generate this matrix. This method looks inefficient because it need to sort and compare columns in every loop.
Can someone please help me about this? Thanks ^_^

Best Answer

Generate all the 5 x 100 samples. Sort by column. transpose. unique() with the 'rows' option, using the multiple-output version of unique(). Use the returned indices to extract the rows of the sorted-transpose that were unique, and the same indices to extract the columns of the original matrix that were unique, saving that partial result.
Now, figure out how many more columns you need, generate that many more, sort them along column, transpose, add them on to the end of the extracted transposed data from the last round, unique that, etc., etc..
Eventually you will not need any more entries.
Alternately:
the probability of any two given columns being duplicates is 5! * 19! / 24! so the cumulative probability over 100 entries is (1 - (1 - factorial(5)*factorial(19)/factorial(24))^100) which is about 1/4 of 1% . You probably won't get any duplicates. You could just choose to try again. Or you could generate a few extra columns the first time around, and take the first 100 non-duplicates.