Remove same element from vector

10 visualizaciones (últimos 30 días)
NA
NA el 30 de Nov. de 2018
Respondida: Andrei Bobrov el 30 de Nov. de 2018
I have this vector
E=[1,2;1,3;1,6;2,3;2,4;3,7;4,5;5,6;5,7;6,7;1,2;2,4;6,8];
I want to omit same element and also (6,8).
As 8 is not conneted to other points, I want to omit it too.
result is: E=[1,2;1,3;1,6;2,3;2,4;3,7;4,5;5,6;5,7;6,7];

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 30 de Nov. de 2018
a = sort(unique(E,'rows'),2);
b = unique(a(:));
c = hist(a(:),b);
out = a(all(ismember(a,b(c > 1)),2),:);

Más respuestas (2)

madhan ravi
madhan ravi el 30 de Nov. de 2018
Editada: madhan ravi el 30 de Nov. de 2018
E=[1,2;1,3;1,6;2,3;2,4;3,7;4,5;5,6;5,7;6,7;1,2;2,4;6,8];
[E,~,~]=unique(E,'rows');
idx=ismember(E,[6 8],'rows');
E=E(~idx,:) %expected result
command window:
>> E
E =
1 2
1 3
1 6
2 3
2 4
3 7
4 5
5 6
5 7
6 7
>>
  1 comentario
NA
NA el 30 de Nov. de 2018
Editada: NA el 30 de Nov. de 2018
But I want a code that recognize [6 8] automatically. find that 8 is not connect to others.

Iniciar sesión para comentar.


Guillaume
Guillaume el 30 de Nov. de 2018
What you have completely failed to mention in your question and left for us to guess is that your E matrix represents the edges of a graph. Without that information, "8 is not connected to other point" is meaningless.
One way to do what you want:
E=[1,2;1,3;1,6;2,3;2,4;3,7;4,5;5,6;5,7;6,7;1,2;2,4;6,8];
g = graph(E(:, 1), E(:, 2)); %remove duplicate edges and make graph
g = simplify(g); %remove duplicate edges and self loops
g = rmnode(g, find(degree(g) <= 1)); %remove isolated nodes or nodes with only one edge
E = g.Edges.EndNodes

Categorías

Más información sobre App Building en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by