How to index all but select indices?

后端 未结 2 1826
不思量自难忘°
不思量自难忘° 2020-12-17 17:06

I have an array a=rand(100), I want to get every value except the values at the indices notidx=[2;50]. Is there a clean way to get a a

相关标签:
2条回答
  • 2020-12-17 17:29

    Update:

    If you are using julia-v0.5+, you can also use the new generator expression, for example:

    view(a, [i for i in indices(a)... if i ∉ notidx])
    

    and

    [a[i] for i in indices(a)... if i ∉ notidx]
    

    Old post:

    To get a copy, you can firstly make a copy of a, then manipulate it with deleteat! to delete those values at specific indices. After you've done this, it's convenient to get a view of a via indexin:

    a = rand(100)         
    # => 100-element Array{Float64,1}:
     0.62636  
     0.488919 
     0.499884 
     ....
    
    b = copy(a)           
    deleteat!(b, [2,50])  
    # => 98-element Array{Float64,1}:
     0.62636  
     0.499884 
     ....
    
    0 讨论(0)
  • 2020-12-17 17:33

    I don't have anything super clean, but you can do

    a[setdiff(1:end, notidx)]
    

    which is slightly cleaner than what you had, or

    ind = trues(length(a))
    ind[notidx] = false
    a[ind]
    

    which is pretty verbose but very clear.

    0 讨论(0)
提交回复
热议问题