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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by