On Fri, Jul 31 2015, Christopher Fisher <[email protected]> wrote:

I was wondering if there is a function for enumerating all of the permutations of size m from n elements, with repetitions allowed. For example, 3 permutations of [1 0] would be [ 1 1 1;1 1 0;1 0 1;0 1 1;1 0 0; 0 1 0;0 0 1; 0 0 0]. (Analogous to http://www.mathworks.com/matlabcentral/fileexchange/11462-npermutek/content/npermutek.m)

Perhaps something like (not tested extensively, check for bugs)

@doc "All length `n` sequences of `1:v` in a matrix, with repetitions."-> function perm_matrix{T}(v::T, n) m = v^n a = zeros(T, m, n) for i = 1:m k = i-1 j = n while ((k > 0) && (j > 0)) (d,r) = divrem(k,v) a[i,j] = r k = d j -= 1 end end a end perm_matrix(3, 2)
Along similar lines, I was wondering if there is a function for permutations without repetitions, such as 2 elements from [1 2 3] is [1 2;1 3;2 3;2 1;3 1;3 2]. I see that there is a permutations function but it only enumerates permutations of the same size as the original set.

Did you have a look at the function Base.combinations? Unless I misunderstand your question, it does what you are asking for.

Best,

Tamas

Reply via email to