2

For example I have A=[11 24 33 47 52 67] and I have indices matrix as I = [2 3] so I want to have the elements of A from the indices other than indices given with I. So I want to have B = [11 47 52 67]. How can I do it and use I as a negated indices matrix?

Eitan T
  • 32,660
  • 14
  • 72
  • 109
erogol
  • 13,156
  • 33
  • 101
  • 155

2 Answers2

4

go for

  idx = logical(ones(size(A)));   % // all indices here

or, as @Gunther Struyf suggests,

  idx = true(size(A));

then

  idx(I) = 0;                       % // excluding not desired indices    
  B = A(idx);                       % // selection

Alternatively

 B = A;
 B(I) = [];
Acorbe
  • 8,367
  • 5
  • 37
  • 66
  • 3
    you can use `true(...)` instead of `logical(ones(...))` ;) and `size` would be better than `length` imo, because it is more general – Gunther Struyf Dec 17 '12 at 17:21
1

You can also make use of setdiff to exclude indices. Here's a one-liner for you:

B = A(setdiff(1:numel(A), I))
Eitan T
  • 32,660
  • 14
  • 72
  • 109