Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 at the other values? I am looking for a good way to do both a copy and a view.

Currently I make the array [1;3:49;51:100] by symdiff(1:100,notidx), but a[symdiff(1:length(a),notidx)] and view(a,a[symdiff(1:length(a),notidx)]) are not very clean (or understandable to others) ways of doing this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
938 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...