Search code examples
matlabmatrixmatrix-indexing

Retrieving the elements of a matrix with negated exact indexing with index matrix?


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?


Solution

  • 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) = [];