Here is my complete (?) General solution using recursion, varargs and splatting:
function expandgrid(args...)
if length(args) == 0
return Any[]
elseif length(args) == 1
return args[1]
else
rest = expandgrid(args[2:end]...)
ret = Any[]
for i in args[1]
for r in rest
push!(ret, vcat(i,r))
end
end
return ret
end
end
eg = expandgrid([1,2,3], ["a","b"], [10,12])
@assert length(eg) == 3*2*2
@show eg
This gives an array of arrays, but you could combine this into a matrix trivially if that is what you wanted.
source
share