Hello
Is it possible to create an array of set of tuple/array?
Say that my array is named M.
I would like to be able to iterate over M[i,j] and that the iterator itself be a tuple/array.
Best,
Michael.
Hello
Is it possible to create an array of set of tuple/array?
Say that my array is named M.
I would like to be able to iterate over M[i,j] and that the iterator itself be a tuple/array.
Best,
Michael.
Hi, welcome to Julia!
Yes, this kind of thing is not too difficult in Julia. The only problem sometimes is working out the syntax (i.e. what you should actually write).
Let’s start by making a simple example: a one-dimensional array of tuples:
M = [(1, 2), (3, 4)]
and iterate through it:
for x in M
println(x)
end
Hopefully this should be enough to get you started. Do ask again if you have more questions.
I am not sure I understand the question, but why not? Eg
julia> genset() = Set(tuple(rand(1:10,3)...) for _ in 1:10)
julia> [genset() for _ in 1:10]
10-element Array{Set{Tuple{Int64,Int64,Int64}},1}:
Set(Tuple{Int64,Int64,Int64}[(1,1,5),(4,2,9),(7,4,1)])
Set(Tuple{Int64,Int64,Int64}[(2,2,4),(7,2,6),(4,10,9)])
Set(Tuple{Int64,Int64,Int64}[(1,8,1),(2,6,5),(9,5,6)])
Set(Tuple{Int64,Int64,Int64}[(3,7,2),(3,9,6),(8,7,7)])
Set(Tuple{Int64,Int64,Int64}[(3,8,1),(9,7,1),(7,2,5)])
Set(Tuple{Int64,Int64,Int64}[(1,10,7),(10,4,4),(3,2,1)])
Set(Tuple{Int64,Int64,Int64}[(10,4,1),(5,8,1),(9,4,3)])
Set(Tuple{Int64,Int64,Int64}[(4,1,1),(4,9,6),(9,7,1)])
Set(Tuple{Int64,Int64,Int64}[(3,1,1),(5,1,9),(9,9,6)])
Set(Tuple{Int64,Int64,Int64}[(4,7,3),(6,1,3),(5,8,4)])
is an Array
of Set
s of Tuple
s.
Thank’s for your answer. So what I need is, for instance, one of the following:
M = Array{Array{Tuple{Int64,Int64},1},2}(i,j),
M = Array{Set{Tuple{Int64,Int64}},2}(i,j)
where i and j are the dimensions of my array. I tried to fill my array with push!, but it does not seem to work:
julia> M = Array{Set{Tuple{Int64,Int64}},2}(1,5)
1×5 Array{Set{Tuple{Int64,Int64}},2}:
#undef #undef #undef #undef #undef
julia> push!(M[1,5],(1,2))
ERROR: UndefRefError: access to undefined reference
in getindex(::Array{Set{Tuple{Int64,Int64}},2}, ::Int64, ::Int64) at ./array.jl:387
The way you initialize your array, it contains undef sets. Push then tries to access those undef and throws an error. Try:
julia> M = [Set{Tuple{Int,Int}}() for i=1:1, j=1:2] # initializes with empty sets
1×2 Array{Set{Tuple{Int64,Int64}},2}:
Set{Tuple{Int64,Int64}}() Set{Tuple{Int64,Int64}}()
julia> push!(M[1,2],(99,99))
Set(Tuple{Int64,Int64}[(99,99)])
julia> M
1×2 Array{Set{Tuple{Int64,Int64}},2}:
Set{Tuple{Int64,Int64}}() Set(Tuple{Int64,Int64}[(99,99)])