Abdul kadir
Abdul kadir

Reputation: 123

Finding elements in an array other than given indices

I want to find the elements in an array which are not the given index elements. For example, given an array A = [1 5 7 8], and index ind = [2 3], the operation should return elements [1 8].

Upvotes: 0

Views: 39

Answers (1)

Use a direct index vector:

B = A(setdiff(1:numel(A),ind));

Or throw away unneeded elements:

B = A;
B(ind) = [];

Or use logical indexing:

% either
B = A(~any(bsxfun(@eq,ind(:),1:numel(A)),1));
% or
B = A(all(bsxfun(@ne,ind(:),1:numel(A)),1));

Upvotes: 2

Related Questions