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