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

1 visualización (últimos 30 días)
HB
HB el 11 de Feb. de 2020
Editada: Stephen23 el 15 de Feb. de 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.

Respuestas (1)

Stephen23
Stephen23 el 11 de Feb. de 2020
Editada: Stephen23 el 11 de Feb. de 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 comentarios
HB
HB el 14 de Feb. de 2020
Editada: HB el 14 de Feb. de 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 el 15 de Feb. de 2020
Editada: Stephen23 el 15 de Feb. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by