除了选择索引外,如何索引所有索引? [英] How to index all but select indices?
问题描述
我有一个数组a=rand(100)
,我想获取除索引notidx=[2;50]
处的值以外的所有值.是否有一种干净的方法可以将a
设置为其他值?我正在寻找一种同时进行复制和查看的好方法.
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.
目前,我是用symdiff(1:100,notidx)
制作数组[1;3:49;51:100]
的,但是a[symdiff(1:length(a),notidx)]
和view(a,a[symdiff(1:length(a),notidx)])
并不是很干净(或其他人可以理解的).
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.
推荐答案
我没有超级干净的东西,但是你可以做到
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]
这很冗长,但很清楚.
这篇关于除了选择索引外,如何索引所有索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!