Generating matrix of integers with distinct rows
Show older comments
I am trying to generate an NxM matrix consisting of integers between 1 and P. Assume M < P. For each row, I would like the M integers to be distinct. This can easily be accomplished by generating each row with the command randperm(P, M). However, in addition to that, I would like for each of the N rows to be a different set of integers (ideally all rows would be distinct under permutations - i.e, if [1,2,3] was a row, I would like to disallow repetitions of [1,2,3] in addition to arbitrary permutations of those integers. This may be too tricky though, in which case I'm fine just excluding [1,2,3]). Is there any simple way of accomplishing this?
For example, let P = 4, and consider trying to generate a 3x2 matrix. The following would be allowed:
1 2
3 4
1 3
but not
1 2
2 1
4 3
Accepted Answer
More Answers (1)
Walter Roberson
on 18 May 2019
0 votes
https://en.m.wikipedia.org/wiki/Combinatorial_number_system
Generate N integers in the range up to combs(P, M). Use the combinatorial number system to go directly to particular combinations. That gives you the unordered content of each row which you can then permute.
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!