# How to extract the common values only between two variables?

10 visualizaciones (últimos 30 días)
Niraj Bal Tamang el 14 de Dic. de 2020
Comentada: Niraj Bal Tamang el 14 de Dic. de 2020
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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (2)

Image Analyst el 14 de Dic. de 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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Niraj Bal Tamang el 14 de Dic. de 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.

Iniciar sesión para comentar.

KSSV el 14 de Dic. de 2020
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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Niraj Bal Tamang el 14 de Dic. de 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.

Iniciar sesión para comentar.

### Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by