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

1 次查看(过去 30 天)
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.

回答(1 个)

Stephen23
Stephen23 2020-2-11
编辑:Stephen23 2020-2-11
>> 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 个评论
HB
HB 2020-2-14
编辑:HB 2020-2-14
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 2020-2-15
编辑:Stephen23 2020-2-15
"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.

请先登录,再进行评论。

类别

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