8
v = range(1e10, -1e10, step=-1e8) # velocities [cm/s]
deleteat!(v, findall(x->x==0,v))

I want to delete the value 0 from v. Following this tutorial, I tried deleteat! but I get the error

MethodError: no method matching deleteat!(::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}, ::Vector{Int64})

What am I missing here?

1
  • In addition to Range not being a Vector, it is important to note that ranges are immutable, so it's not just deleteat! that won't work, but any of the mutating functions, like push!, pop!, splice!, etc. Ranges are clever data structures that work like Vectors for access, does not explicitly store all its elements, and therefore cannot be modified. You always just create a new range.
    – DNF
    Feb 14, 2022 at 8:39

2 Answers 2

7

Use filter! or filter:


julia> filter!(!=(0), [1,0,2,0,4])
3-element Vector{Int64}:
 1
 2
 4

In case of a range you can collect it or use:

julia> filter(!=(0), range(2, -2, step=-1))
4-element Vector{Int64}:
  2
  1
 -1
 -2

However for big ranges you might just not want to materialize them to save the memory footprint. In that case you could use:

(x for x in range(2, -2, step=-1) if x !== 0)

To see what is being generated you need to collect it:

julia> collect(x for x in range(2, -2, step=-1) if x !== 0)
4-element Vector{Int64}:
  2
  1
 -1
 -2
2
  • When I try this with my code, using filter!(!=(0), v), I get setindex! not defined for StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}
    – Dila
    Feb 13, 2022 at 20:21
  • 2
    @Dila Use filter (without the !) instead: v = filter(!=(0), range(1e10, -1e10, step=-1e8)) . An advantage of this method over the collect-then-delete method is that here the Vector is formed directly without the unwanted element (0 in this case). When you collect then deleteat!, the full Vector has to be formed, then the deletion happens and the elements to the right of the 0 (which is half of them) all need to be shifted left to fill in the gap. Using filter avoids that costly two-step process.
    – Sundar R
    Feb 13, 2022 at 22:16
4

Notice the type that is returned by the function range.

typeof(range(1e10, -1e10, step=-1e8))

The above yields to

StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}

Calling the help function for the function deleteat!.

? deleteat!()

deleteat!(a::Vector, inds)

Remove the items at the indices given by inds, and return the > modified a. Subsequent items are shifted to fill the resulting gap.

inds can be either an iterator or a collection of sorted and > unique integer indices, or a boolean vector of the same length as a with true indicating entries to delete.

We can convert the returned type of range using collect. Try the following code.

v = collect(range(1e10, -1e10, step=-1e8))
deleteat!(v,findall(x->x==0,v))

Notice that we can shorten x->x==0 to iszero which yields to

v = collect(range(1e10, -1e10, step=-1e8))
deleteat!(v,findall(iszero,v))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.