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 Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by