# Simple type/array problem

**URL:** <https://discourse.julialang.org/t/simple-type-array-problem/85251>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 3, 2022, 5:19pm UTC](https://discourse.julialang.org/t/simple-type-array-problem/85251 "2022-08-03T17:19:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArminAminian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arminaminian/32/36952_2.png) [@ArminAminian](https://discourse.julialang.org/u/ArminAminian)\
**Post date:** [August 3, 2022, 5:19pm UTC](https://discourse.julialang.org/t/simple-type-array-problem/85251/1 "2022-08-03T17:19:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 3, 2022, 5:44pm UTC](https://discourse.julialang.org/t/simple-type-array-problem/85251/2 "2022-08-03T17:44:56Z")

</div>

Your MWE is not complete:

```julia
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

````julia
```
like this
```

````

and we can help you answer your question.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [August 3, 2022, 6:19pm UTC](https://discourse.julialang.org/t/simple-type-array-problem/85251/3 "2022-08-03T18:19:46Z")

</div>

> [@ArminAminian](#):
>
> dd[2] = sum(q[t,1:5], dims=1);

This is like trying to do the following:

```julia
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.

---

<div class="post-metadata">

**Author:** ![ArminAminian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arminaminian/32/36952_2.png) [@ArminAminian](https://discourse.julialang.org/u/ArminAminian)\
**Post date:** [August 3, 2022, 6:21pm UTC](https://discourse.julialang.org/t/simple-type-array-problem/85251/4 "2022-08-03T18:21:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [August 3, 2022, 6:26pm UTC](https://discourse.julialang.org/t/simple-type-array-problem/85251/5 "2022-08-03T18:26:22Z")

</div>

…and what’s the `t` 😉 ?

If I remove the `t` I can do

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

```

but then

```julia
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
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

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

```
