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

前端 未结 2 1050
醉梦人生
醉梦人生 2021-01-15 13:16

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 ind

相关标签:
2条回答
  • 2021-01-15 13:27

    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) = [];
    
    0 讨论(0)
  • 2021-01-15 13:47

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

    B = A(setdiff(1:numel(A), I))
    
    0 讨论(0)
提交回复
热议问题