Delete rows from matrix that contain values that correspond to values from a separate variable vector

1 view (last 30 days)
Hi,
I wish to remove rows from a matrix A [n x 2] that contains any value from another vector B [1 x n] in its first column. The values in vector B change depending on my problem, I am therefore looking for a solution that can read the values in the B vector and apply the modification automatically to the A matrix.
An example B vector is shown below:
B = [2, 3, 5, 8, 10]
Along with an example A matrix:
A = [ 1 2;
2 3;
2 4;
3 1;
3 4;
4 2;
4 9;
5 1;
5 7;
6 2;
6 3;
6 4;
8 2;
8 9;
9 1;
9 6;
10 6]
The desired output matrix Z [n x 2] would be as follows:
Z = [ 1 2;
4 2;
4 9;
6 2;
6 3;
6 4;
9 1;
9 6]
Any help would be appreciated. I am using this solution as part of a meshing algorithm that I am developing, where matrix Z forms the connectivity between nodes.
Thanks in advance, Sam

Accepted Answer

Birdman
Birdman on 14 Feb 2018
Z=A(~ismember(A(:,1),B),:)

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!