How to extract the common values only between two variables?

23 views (last 30 days)
I have a point file and a polyline file. I want to extract only the lines from the polyline file which contains the points from the other file. Can anyone please help me how to do it? I have attached the points and polyline file.

Answers (2)

Image Analyst
Image Analyst on 14 Dec 2020
Try setdiff() to find out what's not in the other set, and ismember() to find out what's common to both sets.
This may be instructive:
set1 = [1,3,32,19,4,21]
set2 = [2,4,19,25,32,40]
% Find the mismatches which are in set1 but not in set2
mismatches = setdiff(set1, set2)
% Find the mismatches which are in set2 but not in set1
mismatches = setdiff(set2, set1)
% Find out which numbers are in both set2 and in set1
[ia, ib] = ismember(set2, set1)
myMatches = set2(ia)
You'll see:
set1 =
1 3 32 19 4 21
set2 =
2 4 19 25 32 40
mismatches =
1 3 21
mismatches =
2 25 40
ia =
1×6 logical array
0 1 1 0 1 0
ib =
0 5 4 0 3 0
myMatches =
4 19 32
Can you see how to adapt it to your data? If not I'll do it for you.
  1 Comment
Niraj Bal Tamang
Niraj Bal Tamang on 14 Dec 2020
Thank You. I tried this with my data but it showed error. My data consists of two files with spatial attributes (Lat long) instead of regular array of numbers. So when i try to use this, it gave errors.

Sign in to comment.


KSSV
KSSV on 14 Dec 2020
load Points.mat ;
load Polyline.mat ;
m = length(network) ;
n = length(sedsource) ;
P = cell(n,1) ;
iwant = cell(n,1) ;
for i = 1:n
P = [sedsource(i).X ;sedsource(i).Y];
for j = 1:m
L = [network(i).X ;network(i).Y];
idx = ismember(P',L','rows') ;
if idx~=0
iwant{i} = L ;
break
end
end
end
% plot for check
figure
hold on
for i = 1:n
plot(iwant{i}(1,:),iwant{i}(2,:))
plot(P{i}(1),P{i}(2),'*')
end
  1 Comment
Niraj Bal Tamang
Niraj Bal Tamang on 14 Dec 2020
Thank you so much for your answer. The code gave some outputs but i think they are different from what i expected. My point data has 52 values and i assume that when we overlay this point file over the polyline file, we will get 52 lines as output, as the points are distributed such that only one can fall under one line. And the attribute for both the data are spatial (long and lat) so the plot part of the code also didn't work.

Sign in to comment.

Categories

Find more on Tables 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!