Removing rows of a matrix based on rows of another matrix!
10 次查看(过去 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?
0 个评论
采纳的回答
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!