필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
HB
HB 2020년 2월 11일
편집: Stephen23 2020년 2월 15일
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