1

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.

Amro
  • 123,847
  • 25
  • 243
  • 454
Thilo
  • 8,827
  • 2
  • 35
  • 56

2 Answers2

3

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.

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • Yep, this in fact is a - actually quite obvious - way to do it. Probably the way to go. However, at the cost of an additional copy. – Thilo Mar 13 '13 at 09:29
  • You don't have to use a copy, if you're okay with modifying `v` itself. – Eitan T Mar 13 '13 at 09:30
0

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 );
Shai
  • 111,146
  • 38
  • 238
  • 371