Created
July 31, 2015 11:13
-
-
Save tpapp/43131b48f1afd97f1018 to your computer and use it in GitHub Desktop.
permutations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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