This question already has answers here:
Retrieving the elements of a matrix with negated exact indexing with index matrix?
(2 answers)
Closed 6 years ago.
I have a column vector:
A = [1; 2; 3; 4; 4; 5; 5; 7];
I wish to exclude the elements of A
that are in a second matrix B
:
B = [4; 5]
The final result should be:
A = [1; 2; 3; 7]
I am pretty sure using MATLAB elegant syntaxing, this be accomplished without writing a for
loop, but not sure how?
I would use Afilt=A(~ismember(A,B));
. This will return all elements of A
which are not in B
.
You can compare values with bsxfun
:
A = A(all(bsxfun(@ne, A(:), B(:).'), 2))
This approach is especially good if you need to handle floating-point numbers (whereismember
fails):
A(all(abs(bsxfun(@minus, A(:), B(:).')) >= eps, 2))
Instead of eps
, you can set any tolerance threshold you want.
EDIT: If you want to remove row 4 and 5 it is this, if you want to remove the rows with fours and fives check the other answers.
Simple as this
A = [1; 2; 3; 4; 4; 5; 5; 7];
B = [4; 5];
A(B)=[];