# \`convert\` an object of type Array{Any,1} to an object of type Float64

**URL:** https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523
**Category:** New to Julia
**Created:** [March 30, 2019, 1:30am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523 "2019-03-30T01:30:53Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 1:30am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/1 "2019-03-30T01:30:53Z")

</div>

Dear All,

is it possible that we `convert` an object of type Array{Any,1} to an object of type Float64:

```julia
rb = [0.0 0.0 1.0 1.0]
i=1
while i < length(rb)
  if (rb[i]==rb[i+1])
    rb[i+1]=[]
   global i=i-1
   end
   global i=i+1
end

```

Error:  
**`ERROR: LoadError: MethodError: Cannot `convert` an object of type Array{Any,1} to an object of type Float64`**

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [March 30, 2019, 1:53am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/2 "2019-03-30T01:53:13Z")

</div>

I’m confused as to what you are trying to do. The reason you have a `Array{Any,1}` is because `[]` is an empty Vector without a type, and thus of type `Any`.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 1:56am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/3 "2019-03-30T01:56:05Z")

</div>

> I’m confused as to what you are trying to do.

Answer:  
**Delete repeated values in the vector.**

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [March 30, 2019, 2:02am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/4 "2019-03-30T02:02:35Z")

</div>

If you only need the first unique of each value, you can use `unique!(rb)`.

EDIT: My alternative method had a mistake.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 2:05am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/5 "2019-03-30T02:05:24Z")

</div>

The corret answer: is

> rb =[0.0 1.0]

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 2:10am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/6 "2019-03-30T02:10:58Z")

</div>

> [@Daniel\_Berge](#):
>
> `unique!(rb)` .

` unique!(rb)`

```julia
ERROR: MethodError: no method matching _unique!(::Array{Float64,2})

You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.
Closest candidates are:
  _unique!(::Any, ::AbstractArray{T,1} where T, ::Any, ::Set, ::Any) at set.jl:185
  _unique!(::Any, ::AbstractArray{T,1} where T, ::Set, ::Integer, ::Integer) at set.jl:248
  _unique!(::AbstractArray{T,1} where T) at set.jl:270
Stacktrace:
 [1] unique!(::Array{Float64,2}) at .\set.jl:343
 [2] top-level scope at none:0

```

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [March 30, 2019, 2:20am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/7 "2019-03-30T02:20:38Z")

</div>

You entered a 2 dimension Array, rather than a 1D dimension

```julia
rb = [0.0 0.0 1.0 1.0]

```

If you want to enter a vector, you need the `,`

```julia
rb = [0.0, 0.0, 1.0, 1.0]
unique(rb) == [0.0, 1.0]

```

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 2:25am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/8 "2019-03-30T02:25:10Z")

</div>

thanks for your sharing anther method (command in julia) , but If I still want to use own loops in Julia, is it not possible and why we can not archive this simple loop in Julia?

```julia
rb = [0.0 0.0 1.0 1.0]
i=1
while i < length(rb)
  if (rb[i]==rb[i+1])
    rb[i+1]=[]
   global i=i-1
   end
   global i=i+1
end

```

---

<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: [March 30, 2019, 2:40am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/9 "2019-03-30T02:40:09Z")

</div>

```julia
julia> rb = [0.0, 0.0, 1.0, 1.0]
4-element Array{Float64,1}:
 0.0
 0.0
 1.0
 1.0

julia> i=1
1

julia> while i < length(rb)
         global rb
         if (rb[i]==rb[i+1])
           rb = rb[1:length(rb) .!= i]
          global i=i-1
          end
          global i=i+1
       end

julia> rb
2-element Array{Float64,1}:
 0.0
 1.0

```

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [March 30, 2019, 2:41am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/10 "2019-03-30T02:41:35Z")

</div>

This will skip the duplicates.

```julia
function removeduplicates(a)
    y=[a[1]]
    for i=2:length(a)
        a[i]!=y[end] && push!(y,a[i])
    end
    y
end

```

In Julia, you can’t remove from a vector by `rb[i+1]=[]`. You can either `pop!` to remove the last item, or `deleteat!` to remove a specific index. These functions do not work on a `Array{T,2}` because it is not logical to remove an item from an `N x M` matrix. Along with `unique!`, the `!` indicates that it operates in place, similar to how your original function operates. `rb = [0.0 0.0 1.0 1.0]` is not a 1 dimensional Vector, but a 2D Matrix of size 1x4.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 2:44am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/11 "2019-03-30T02:44:24Z")

</div>

@Daniel_Berge and @Elrod,

Excellent. Thank you!

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [March 30, 2019, 2:48am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/12 "2019-03-30T02:48:19Z")

</div>

I think you’re just looking for the `deleteat!` function:

```julia
function delete_repeated_sequential!(rb)
    i = 1
    while i < length(rb)
        if (rb[i] == rb[i + 1])
            deleteat!(rb, i + 1)
        else
            i += 1
        end
    end
    rb
end

delete_repeated_sequential!([0.0, 0.0, 1.0, 1.0])

```

Edit: ah, this was hinted at earlier.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 30, 2019, 2:51am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/13 "2019-03-30T02:51:01Z")

</div>

I think in while loop, we have to use global i (Julia 1.1.0)?

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [March 30, 2019, 2:55am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/14 "2019-03-30T02:55:52Z")

</div>

Only in global scope, not inside a function. In general, it’s a good idea to use functions. One reason is that it affords the compiler many more optimization opportunities. See [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables-1).

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [March 30, 2019, 3:15am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/15 "2019-03-30T03:15:30Z")

</div>

For efficiency reasons, I’d go the classical way of keeping a Boolean vector of valid indices and indexing into it.

```julia
function delete_successive(rb)
    indx = trues(length(rb))
    for i = 2:length(rb)
        indx[i] = rb[i] != rb[i-1]        
    end 
    rb[indx]
end  

rb = [0.0 0.0 1.0 1.0];
julia> delete_successive(rb)
2-element Array{Float64,1}:
 0.0
 1.0

```

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [March 30, 2019, 4:41am UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/16 "2019-03-30T04:41:47Z")

</div>

Just for fun, a slightly more advanced way to solve this problem in Julia is to define your own iterator:

```julia
struct SkipRepeated{I}
    inner::I
end

# See https://docs.julialang.org/en/v1/base/collections/#lib-collections-iteration-1
function Base.iterate(x::SkipRepeated)
    state = iterate(x.inner)
    state === nothing && return nothing
    (val, _) = state
    (val, state)
end

function Base.iterate(x::SkipRepeated, state)
    (previous, innerstate) = state
    while true
        state = iterate(x.inner, innerstate)
        state === nothing && return nothing
        (val, innerstate) = state
        val != previous && return (val, state)
    end
end

Base.IteratorSize(::Type{<:SkipRepeated}) = Base.SizeUnknown()
Base.IteratorEltype(::Type{SkipRepeated{I}}) where {I} = Base.IteratorEltype(I)
Base.eltype(x::SkipRepeated) = eltype(x.inner)

```

which you can use as follows:

```julia
julia> rb = [1 1 2 3 3 3 0] # also works for your original row vector
1×7 Array{Int64,2}:
 1 1 2 3 3 3 0

julia> for x in SkipRepeated(rb)
            # simply iterating over SkipRepeated(rb) doesn't allocate a new vector
           @show x
       end

x = 1
x = 2
x = 3
x = 0

julia> collect(SkipRepeated(rb))
4-element Array{Int64,1}:
 1
 2
 3
 0

```

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 5, 2020, 2:56pm UTC](https://discourse.julialang.org/t/convert-an-object-of-type-array-any-1-to-an-object-of-type-float64/22523/17 "2020-10-05T14:56:14Z")

</div>

A post was split to a new topic: [Help with convert error from Array{Float64, 2} to Float64](https://discourse.julialang.org/t/help-with-convert-error-from-array-float64-2-to-float64/47792)
