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

北战南征 提交于 2019-12-19 10:26:43

问题


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?


回答1:


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



回答2:


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

B = A(setdiff(1:numel(A), I))


来源:https://stackoverflow.com/questions/13918790/retrieving-the-elements-of-a-matrix-with-negated-exact-indexing-with-index-matri

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