Removing rows of a matrix based on rows of another matrix!

8 次查看(过去 30 天)
Imagine I have two matrices with different sizes (let's say 6x2 and 5x2) as follows:
A = [47 10;
29 10;
23 10;
34 10;
12 10;
64 10];
B = [23 20;
12 20;
54 20
47 20;
31 20];
I need to compare A(:,1) with B(:,1) and delete the rows in matrix A whose first-column-element is different from matrix B's first-column-element (so my focus is only on first columns of the matrices). So I should eventually get something like this:
A = [47 10;
12 10;
23 10];
as "47", "12", and "23" are the only first-column-elements in A that also exist in B! I have written this but I get the error "Matrix dimensions must agree."!
TF = A(:,1) ~= B(:,1);
A(TF,:) = [];
Any ideas how I could fix this?

采纳的回答

Birdman
Birdman 2018-1-17
A=A(ismember(A(:,1),B(:,1)),:)

更多回答(1 个)

Steven Lord
Steven Lord 2018-1-17
Since it doesn't seem like ordering matters [you don't require A(n, 1) to match B(n, 1) as long as it matches some element in B(:, 1)] use the ismember function.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by