필터 지우기
필터 지우기

How to extract the common values only between two variables?

조회 수: 18 (최근 30일)
Niraj Bal Tamang
Niraj Bal Tamang 2020년 12월 14일
댓글: Niraj Bal Tamang 2020년 12월 14일
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.

답변 (2개)

Image Analyst
Image Analyst 2020년 12월 14일
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
Niraj Bal Tamang
Niraj Bal Tamang 2020년 12월 14일
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.

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2020년 12월 14일
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
Niraj Bal Tamang
Niraj Bal Tamang 2020년 12월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by