Remove same element from vector

조회 수: 12 (최근 30일)
NA
NA 2018년 11월 30일
답변: Andrei Bobrov 2018년 11월 30일
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];

채택된 답변

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

추가 답변 (2개)

madhan ravi
madhan ravi 2018년 11월 30일
편집: madhan ravi 2018년 11월 30일
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
NA
NA 2018년 11월 30일
편집: NA 2018년 11월 30일
But I want a code that recognize [6 8] automatically. find that 8 is not connect to others.

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


Guillaume
Guillaume 2018년 11월 30일
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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by