Hello!
I am a fairly new Julia user and have been dabbling with the language on and off during the last year. I am currently working on my first bigger project and have run into issues with list comprehension.
Here’s a minimum working example that will produce the error:
using DSP
window_func(n) = sin.(pi/n*(0:n-1))
function fails()
# just random data
data = rand(Complex{Float64}, 46080)
# I'm suspecting the return type Periodograms.Spectrograms to cause issues
spec = Periodograms.spectrogram(data, 512, 384, fs=375, window=window_func)
@show typeof(spec.power)
cols = 1:2:162
rows = [rand(1:10, 20), rand(1:10, 20)]
@show typeof(cols)
@show typeof(rows)
# throws error
[[spec.power[r, c] for (r, c) in zip(row, cols)] for row in rows]
end
function works()
array = rand(1.0:100.0, 10, 10)
@show typeof(array)
cols = collect(1:10)
rows = [rand(1:10, 10), rand(1:10, 10)]
@show typeof(cols)
@show typeof(rows)
[[array[r, c] for (r, c) in zip(row, cols)] for row in rows]
end
Running it produces the following outputs:
julia> include("forum.jl")
works (generic function with 1 method)
julia> fails()
typeof(spec.power) = Array{Float64,2}
typeof(cols) = StepRange{Int64,Int64}
typeof(rows) = Array{Array{Int64,1},1}
ERROR: DimensionMismatch("dimensions must match")
Stacktrace:
[1] collect(::Base.Generator{Base.Iterators.Zip{Tuple{Array{Int64,1},StepRange{Int64,Int64}}},getfield(Main, Symbol("##20#22")){DSP.Periodograms.Spectrogram{Float64,Frequencies}}}) at ./indices.jl:154
[2] collect(::Base.Generator{Array{Array{Int64,1},1},getfield(Main, Symbol("##19#21")){DSP.Periodograms.Spectrogram{Float64,Frequencies},StepRange{Int64,Int64}}}) at ./none:0
[3] fails() at /home/galvanix/Projects/julia/wspr/forum.jl:21
[4] top-level scope at none:0
julia> works()
typeof(array) = Array{Float64,2}
typeof(cols) = Array{Int64,1}
typeof(rows) = Array{Array{Int64,1},1}
2-element Array{Array{Float64,1},1}:
[61.0, 51.0, 76.0, 26.0, 83.0, 1.0, 25.0, 16.0, 10.0, 10.0]
[49.0, 70.0, 64.0, 46.0, 31.0, 47.0, 2.0, 33.0, 83.0, 78.0]
I am at a loss here as to why one works and the other doesn’t. Looking at the types of the arrays involved everything seems to be identical?
Can anyone explain this behaviour?