# MethodError: Cannot \`convert\` an object of type Vector{Float64} to an object of type Float64

**URL:** <https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 13, 2021, 11:03am UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335 "2021-08-13T11:03:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 13, 2021, 11:03am UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/1 "2021-08-13T11:03:21Z")

</div>

What does this error mean?

That an object somewhere in the code is specified as `Vector{Float64}` and Julia cannot convert it to `Float64`? I thought it didn’t matter so long as the type was matched (i.e. both Float64).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 13, 2021, 11:09am UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/2 "2021-08-13T11:09:39Z")

</div>

```julia
julia> a = 3.0;

julia> convert(Float32, a)
3.0f0

julia> convert(Float32, [a, a])
ERROR: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float32
Closest candidates are:

```

you cannot convert an array into a scalar and it doesn’t make sense anyway

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [August 13, 2021, 12:03pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/3 "2021-08-13T12:03:17Z")

</div>

A common cause is `setindex!`:

```julia
julia> x = rand(3);

julia> x[1] = [1.0];
ERROR: MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float64

```

---

<div class="post-metadata">

**Author:** ![Beboleto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beboleto/32/31835_2.png) [@Beboleto](https://discourse.julialang.org/u/Beboleto)\
**Post date:** [December 11, 2021, 3:54pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/4 "2021-12-11T15:54:12Z")

</div>

Hi everybody,  
I have a similar question to the extent that when I use setindex! Julia raises the same error  
My script:

 ![JuliaIssue](https://global.discourse-cdn.com/julialang/original/3X/8/b/8b33b952f3f7a57cb55fa6568c6bbf75ace8ad51.png)

---

<div class="post-metadata">

**Author:** ![Beboleto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beboleto/32/31835_2.png) [@Beboleto](https://discourse.julialang.org/u/Beboleto)\
**Post date:** [December 11, 2021, 3:56pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/5 "2021-12-11T15:56:46Z")

</div>

The error message :

 ![JuliaIssue](https://global.discourse-cdn.com/julialang/original/3X/c/2/c263fd85e48822b525c77c4d9d7555f96cb3b0ac.png)

Do you have any clue ?

Tanks in advance !

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 11, 2021, 4:40pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/6 "2021-12-11T16:40:56Z")

</div>

Probably best to start a separate thread about your issue. That said, your error is relatively clear I would have thought - you’re trying to set a value of `yhat`, but you’re passing a vector, which is what `[1 1]*xhat` returns, as `xhat` is a two-element vector:

```julia
julia> [1 1]*rand(2)
1-element Vector{Float64}:
 1.074598145683848

```

If you just want to sum the elements of `xhat` why not just use `sum`? I probably would have written something like:

```julia
y = [0 1 2.5 4.1 5.8 7.5]
x̂ = [0, 0]
n = 100
ϵ = 0.01
ŷ = zeros(5)

for k in 1:n
	for l in 1:5
		ϵ₁, ϵ₂ = rand(2)
		A = [1 0; ϵ₁ 0.3]
		B = [ϵ₂; 1 - ϵ₂]
		x̂ = A*x̂ + B
		ŷ[l] = sum(x̂)
	end

	if ŷ*y < ϵ
		scatter(x̂[1], x̂[2], aspect_ratio = 1)
	else
		scatter!(x̂[1], x̂[2], aspect_ratio = 1)
	end
	
	ŷ = zeros(5)
end

```

Are you coming from MATLAB by any chance? Make sure to read the Julia docs, including the section on notable differences from MATLAB, to make your life easier.

---

<div class="post-metadata">

**Author:** ![Beboleto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beboleto/32/31835_2.png) [@Beboleto](https://discourse.julialang.org/u/Beboleto)\
**Post date:** [December 11, 2021, 8:24pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/7 "2021-12-11T20:24:58Z")

</div>

@nilshg thank you very much for your quick answer  
I got your point, however what would be the general case ? Like in the photo below ?  
 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/c/8/c89e6f9b2dd73c0a5f1525069ca46e8b009f20d9.png)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 11, 2021, 8:32pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/8 "2021-12-11T20:32:32Z")

</div>

what general case? are you look for element wise set index? `.=` ?

please stop sending screenshots and try to send MWE (minimal working example) snippet in the future

---

<div class="post-metadata">

**Author:** ![Beboleto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beboleto/32/31835_2.png) [@Beboleto](https://discourse.julialang.org/u/Beboleto)\
**Post date:** [December 11, 2021, 9:06pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/9 "2021-12-11T21:06:41Z")

</div>

What I mean by general case is to set a value of an element of a matrix with a dot product (here [m1 m2] and xhat)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 11, 2021, 9:39pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/10 "2021-12-11T21:39:42Z")

</div>

the dot product will be calculated first. it should give you a scalar, you can set an element of a matrix to a scalar regardless how you got that scalar.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 11, 2021, 9:46pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/11 "2021-12-11T21:46:07Z")

</div>

In Julia a dot product is computed by `dot(x,y)` or `x ⋅ y`, not `x' * y` like in Matlab. E.g.

```julia
julia> using LinearAlgebra

julia> dot([1 ; 2], [3; 4])
11

julia> [1 ; 2] ⋅ [3; 4]
11

julia> A = zeros(2,2)
2×2 Matrix{Float64}:
 0.0 0.0
 0.0 0.0

julia> A[1,1] = [1 ; 2] ⋅ [3; 4]
11

```

Do read this documentation. It’ll save you a whole lot of trouble. [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#:~:text=Julia%20has%20push!%20and%20append,instead%20of%20floating%20point%20numbers).

---

<div class="post-metadata">

**Author:** ![Beboleto](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beboleto/32/31835_2.png) [@Beboleto](https://discourse.julialang.org/u/Beboleto)\
**Post date:** [December 11, 2021, 10:00pm UTC](https://discourse.julialang.org/t/methoderror-cannot-convert-an-object-of-type-vector-float64-to-an-object-of-type-float64/66335/12 "2021-12-11T22:00:02Z")

</div>

Thank you all for your help!  
I’ll check your documentation!
