MATLAB: Excluding some elements from a Vector

elementvectors

qw=[1:0.4:50]';
c=[9,17,25, 31,43]'
I wish to exclude all the elements that are c-0.5 to c+0.5

Best Answer

qw = 1:0.4:50;
c = [9;17;25;1;43];
tol = .5;
(a) Bsxfun solution
Out = qw(~any(bsxfun(@ge, qw, c-tol) & bsxfun(@le, qw, c+tol)));
(B) Arrayfun solution
Out = qw(~arrayfun(@(x) any(x >= c-tol & x <= c+tol),qw))