whyharsha
whyharsha

Reputation: 87

Why is the function not matching the call?

I have a function blur_1D(v, l) which takes a vector v and an integer l and for each value v[i] in v, it gets the mean of i-l to i+l and replaces v[i] to create a blur. My function isn't getting matched to the call. Here's the code.

function mean(x)
    sum = 0.0

    for i in 1:length(x)
        sum += x[i]
    end

    return sum / length(x)
end
function extend(v, i)
    n = length(v)
    
    if i < 1
        return v[1]
    elseif i > n
        return v[n]
    else
        return v[i]
    end
end
function blur_1D(v, l)
    blur_v = zeros(typeof(v[1]), length(v))
    
    for i in 1:length(v)
        box = zeros(typeof(v[i]), ((2*l)+1))
        k = 1
        
        for j in i-l:i+l
            box[k] = extend(v, j)
            k += 1
        end
        
        blur_v[i] = mean(box)
    end
    
    return blur_v
end
n = 100
v = rand(n)
begin
    colored_line(x::Vector{<:Real}) = Gray.(Float64.((hcat(x)')))
    colored_line(x::Any) = nothing
end
colored_line(blur_1D(v))

Why does it give me an error?

MethodError: no method matching blur_1D(::Array{Float64,1})
Closest candidates are:
blur_1D(::Any, !Matched::Any) at /Users/...

Please excuse any inefficient, inelegant code/syntax, but I do welcome suggestions on how I could improve that as well. :)

Upvotes: 1

Views: 94

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42264

Perhaps the l parameter in your blur function has some default value and you normally want to use a one-parameter version.

In that case you should define function with a default value:

function blur_1D(v, l=0)

BTW, I strongly discourage using l for variable name because it can be easily be mistaken with 1 (one), especially when the code is read by somebody else.

Upvotes: 3

Related Questions