# Simple type/array problem

d = [1.3, 2., 3., 4.3]
dd =[1., 3.]
dd[2] = sum(q[t,1:5], dims=1);

This gives the following errow:

MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float64
Closest candidates are:

• convert(::Type{T}, ::T) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:6*
• convert(::Type{T}, ::Number) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/number.jl:7*
• convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/base/twiceprecision.jl:262*

If i siply put
dd[2] = 2;
it works!

Thank you for helping me to fix this!

Your MWE is not complete:

``````julia> d = [1.3, 2., 3., 4.3]
4-element Vector{Float64}:
1.3
2.0
3.0
4.3

julia> dd =[1., 3.]
2-element Vector{Float64}:
1.0
3.0

julia> dd[2] = sum(q[t,1:5], dims=1);
ERROR: UndefVarError: q not defined
Stacktrace:
[1] top-level scope
@ REPL[3]:1
``````

Post a runnable MWE with triple back-ticks

`````````
like this
```
``````

1 Like

This is like trying to do the following:

``````julia> dd = [1.0, 3.0];

julia> dd[2] = [1, 2, 3, 4, 5]
ERROR: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type Float64
Closest candidates are:
convert(::Type{T}, ::T) where T<:Number at ~/programs/julia/julia-1.7.0/share/julia/base/number.jl:6
convert(::Type{T}, ::Number) where T<:Number at ~/programs/julia/julia-1.7.0/share/julia/base/number.jl:7
convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at ~/programs/julia/julia-1.7.0/share/julia/base/twiceprecision.jl:262
...
Stacktrace:
[1] setindex!(A::Vector{Float64}, x::Vector{Int64}, i1::Int64)
@ Base ./array.jl:903
[2] top-level scope
@ REPL[2]:1
``````

I.e., `sum(q[t,1:5], dims=1)` results in an `Array`, not something that can be converted into a `Float64`.

EDIT: If `t` is a scalar, then `q[t,1:5]` is a `Vector`, in which case you can omit the `dims` keyword argument if you actually want the scalar output of the summation.

sorry I changed the example for the question. in the sum function q is supposed to be d.

â€¦and whatâ€™s the `t` ?

If I remove the `t` I can do

``````d = [1.3, 2., 3., 4.3]
dd = [1., 3.]
``````

but then

``````sum(d[1:4], dims=1)
1-element Vector{Float64}:
10.6
``````

since summing over dimensions â€ścollapsesâ€ť them to size 1, but does still keep the array structure (vector here)

For a vector you could

``````julia> sum(d)
10.6
``````

to actually get a number. This can be put into `dd[2]` (where you just can â€śstoreâ€ť numbers but not vectors, since `dd` is a vector of numbers).

So one solution (depending on your larger setting) could be

``````dd[2] = sum(d)
``````