Skip to content

Instantly share code, notes, and snippets.

@tpapp
Created July 31, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpapp/43131b48f1afd97f1018 to your computer and use it in GitHub Desktop.
Save tpapp/43131b48f1afd97f1018 to your computer and use it in GitHub Desktop.
permutations
@doc "All length `n` permutations of `v` in a matrix, with repetitions."->
function perm_matrix{T}(v::Vector{T}, n)
l = length(v)
m = l^n
a = Array(T, m, n)
for i = 1:m
k = i-1
j = n
while (j > 0)
(d,r) = divrem(k,l)
a[i,j] = v[r+1]
k = d
j -= 1
end
end
a
end
function permuted_combinations{T}(v::Vector{T},k)
l = length(v)
m1 = binomial(l,k)
m2 = factorial(k)
a = Array(T, m1*m2, k)
j = 0
for c in combinations([2,3,5],2)
for p in permutations(1:k)
j += 1
a[j,:] = c[p]
end
end
a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment