This should be trivial but I’m stumped. Simple code snippet:
# points = Array{(UInt8, UInt8, UInt8)}(undef, x_size, y_size)
points = Array{undef,(6, 4)} # compiles, but not valid
val = (100, 225, 255)
points[2][3] = val # Fails on this line
Thanks
julia> val = (100, 225, 255)
(100, 225, 255)
julia> T = typeof(val) #use this info to determine type of array element
Tuple{Int64, Int64, Int64}
julia> points = Array{T, 2}(undef, 3,3)
3×3 Matrix{Tuple{Int64, Int64, Int64}}:
(0, 0, 0) (0, 0, 0) (0, 0, 0)
(0, 0, 0) (0, 0, 0) (0, 0, 0)
(0, 0, 0) (0, 0, 0) (0, 0, 0)
julia> points[2,3] = val
(100, 225, 255)
julia> points
3×3 Matrix{Tuple{Int64, Int64, Int64}}:
(0, 0, 0) (0, 0, 0) (0, 0, 0)
(0, 0, 0) (0, 0, 0) (100, 225, 255)
(0, 0, 0) (0, 0, 0) (0, 0, 0)
2 Likes
Matrix{NTuple{3, UInt8}}(undef, xsize, ysize)
(UInt8, UInt8, UInt8)
isn’t a tuple type, but a tuple of types.
2 Likes
Alternatively,
Array{Tuple{UInt8, UInt8, UInt8}}(undef, x_size, y_size)
1 Like
All 3 suggestions worked like a charm and were great.
I did have to change the points[2][3] = val % hangover from Python
to points[2, 3] = val
Many thanks. Harvey
Yeah, numpy
lets you treat a matrix as if it were also a list of lists, but in Julia those are separate concepts and therefore separate types. It does take a bit of getting used to, but I think it’s worth it.
1 Like
I forgot to mention, Jliing hit a key issue. The elements of val were of type Int64, not UInt8.
Again, many thanks.
You can do this in Python, but I think that points[2, 3]
is the most idiomatic numpy.