Complement subset in Matlab [duplicate]

為{幸葍}努か 提交于 2020-01-21 23:01:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!