Effacer les filtres
Effacer les filtres

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

6 vues (au cours des 30 derniers jours)
Antonio
Antonio le 17 Jan 2018
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?

Réponse acceptée

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

Plus de réponses (1)

Steven Lord
Steven Lord le 17 Jan 2018
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.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by