Remove same element from vector

1 view (last 30 days)
NA
NA on 30 Nov 2018
Answered: Andrei Bobrov on 30 Nov 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];

Accepted Answer

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

More Answers (2)

madhan ravi
madhan ravi on 30 Nov 2018
Edited: madhan ravi on 30 Nov 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 Comment
NA
NA on 30 Nov 2018
Edited: NA on 30 Nov 2018
But I want a code that recognize [6 8] automatically. find that 8 is not connect to others.

Sign in to comment.


Guillaume
Guillaume on 30 Nov 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

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!