问题
In R, I can do the following:
v <- 11:20
v[-(4:5)]
and get 11 12 13 16 17 18 19 20
, thus all indices except the 4th and 5th.
Is there an equivalent in Matlab's indexing logic?
However I wrap my mind around it, I do not seem to get the correct search terms to google my own result for this fairly elementary question.
Note: Of course I might use some of the set functions, e.g.
v = 11:20;
v(setdiff(1:length(v), 4:5))
However, this just is not intuitive.
回答1:
An alternative is to simply remove the elements from the array:
u = v;
u(4:5) = [];
I'm using a temporary variable since I don't know if it's acceptable to modify the original array v
or not.
回答2:
I don't think there is an elegant way, but a more performant might be
v = rand(1,10);
sel = true(1, numel(v));
sel( 4:5 ) = false;
v = v( sel );
来源:https://stackoverflow.com/questions/15381188/complement-subset-in-matlab