Effacer les filtres
Effacer les filtres

Delete values in one matrix that aren't contained in a second matrix

1 vue (au cours des 30 derniers jours)
HB
HB le 11 Fév 2020
Modifié(e) : Stephen23 le 15 Fév 2020
Hello
I have two matrices. Matrix 1 which is 189349x4 containing X, Y, Z and a variable respectively. Matrix 2 which is 81102x3 containing X, Y and Z respectively. Please see attached .txt files of the matrices. The X Y Z values of matrix 2 are contained in matrix 1. I want delete the X Y Z values in matrix 1 that arent contained in matrix 2.
What would be the easiest way to achieve this?
Thanks in advance.

Réponses (1)

Stephen23
Stephen23 le 11 Fév 2020
Modifié(e) : Stephen23 le 11 Fév 2020
>> M1 = dlmread('coordiates_variable.dat');
>> M2 = dlmread('coordinates.txt');
>> idx = ismembertol(M1(:,1:3),M2, 0.0001, 'ByRows',true); % select the tolerance to suit your needs.
>> M1(~idx,:) = [];
  3 commentaires
HB
HB le 14 Fév 2020
Modifié(e) : HB le 14 Fév 2020
I think my initial explanation may have been a bit confusing so apologies.
In your suggested script instead of going ‘byrows’ is there a way of going just by the first column. So for instance can I do a search of column 1 of M1 and if there is a value that matches a value in column 1 of M2 then all those columns of that row in M1 are extracted to a new matrix.
Stephen23
Stephen23 le 15 Fév 2020
Modifié(e) : Stephen23 le 15 Fév 2020
"However there seems to be a lot of other values missing too...."
Most of the X Y Z triples in M2 do not exist in M1, even taking into account some tolerance. Ergo, following your original request "I want delete the X Y Z values in matrix 1 that arent contained in matrix 2", most of the rows of M1 should be removed.
"In your suggested script instead of going ‘byrows’ is there a way of going just by the first column."
Of course, you can just compare the first columns, if that is what you really want to do:
idx = ismembertol(M1(:,1),M2(:,1), 0.0001);
Of course this only compares the X values, unlike what you original question asked for (and what my answer provided).
"..if there is a value that matches a value in column 1 of M2..."
Your data consist of fractional values which are unlikely to match exactly, which is why I used ismembertol.

Connectez-vous pour commenter.

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