How to delete/extract rows of data from file 1 based on what data appears in file 2?

조회 수: 1(최근 30일)
Chris F
Chris F 2018년 5월 26일
편집: dpb 2018년 5월 26일
Version 2018a. The goal is to remove all unwanted data or to extract just the desired rows.
I've got two sets of data (D1 and D2) that are made up of an equal number of columns. The number of rows of each data set may be different.
The values in the second column vary slightly between both sets. However, if a number in the second column from D1 also appears somewhere in column two from D2, extract both of those rows. Alternatively, delete all rows from D1 and D2 with a value in column two that does not have an equal.
I've made some progress using ismember and intersect, but I'm hitting a wall trying to apply the row indices in a loop. Maybe there's a cleaner way to go about this?
PS: Here's a more advanced question. If a value in col 2 of D1 is within 10% of a value in col 2 of D2, then accept each of those rows to compare. I struggle with this because what if there's a tie for multiple rows. How would you determine a preference?

채택된 답변

dpb
dpb 2018년 5월 26일
편집: dpb 2018년 5월 26일
A.
in1=ismember(D1(:,2),D2(:,2)); % locations of D1 in D2
in2=ismember(D2(:,2),D1(:,2)); % vice versa
D=[D1(in1,:); D2(in2,:)]; % smoosh the two together...
B. See
doc ismembertol

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by