Here is a MWE:
D = 3; L = 2000
vectors = [rand(L) for i in 1:D]
d = Dict{Vector{Int}, Int}()
minima = [minimum(v) for v in vectors]
ind = zeros(Int, D)
ε = 0.001
for j in 1:length(vectors[1])
for k in 1:D
ind[k] = Int(ceil((vectors[k][j] - minima[k])/ε))
end
haskey(d, ind) || (d[ind] = 0)
d[ind] += 1
if d[ind] == 0
error("For ind=$ind, the dict had 0!")
end
end
#return [val/L for val in values(d)]
vals = values(d)
RL = length(vals)
ret = zeros(Float64, RL)
i = 1
for val in vals
val == 0 && error("val was 0")
(x = val/L; x == 0) && error("val/L was 0")
ret[i] = x
i += 1
end
Running this code gives you error “val was 0”.
So what is happening here is that I get a dictionary with some of its values are 0. However, this cannot be correct, because the dictionary values are set with the lines:
haskey(d, ind) || (d[ind] = 0)
d[ind] += 1
therefore, if a key exists, its value must be at least 1.
What is happening here?