Delete values in one matrix that aren't contained in a second matrix
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Stephen23
on 11 Feb 2020
Edited: Stephen23
on 11 Feb 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 Comments
Stephen23
on 15 Feb 2020
Edited: Stephen23
on 15 Feb 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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!