How to eliminate subset of paths?

조회 수: 2 (최근 30일)
Hari
Hari 2016년 10월 27일
댓글: Hari 2016년 10월 31일
I have a cell array of paths stored as a variable:
[1,2,3,6,8,10]
[1,2,4,6,8,10]
[1,2,3,6,8,10,11]
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,13]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,15]
[1,2,3,6,8,15]
[2,1]
[2,3]
[2,4]
[2,5]
[2,4,5]
[2,3,6]
Is there a way to remove those paths which are subsets of other paths? For eg: In this case [1,2,3,6,8,10] is a subset of [1,2,3,6,8,10,11] and can hence be removed. Similarly [1,2,4,6,8,10] can be removed. But [1,2,3,6,15] is not a subset of [1,2,3,6,8,15]. So the matlab functions like 'ismember' cannot be used. The end result should be:
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,13]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,15]
[1,2,3,6,8,15]
[2,1]
[2,5]
[2,4,5]
[2,3,6]
Thank you for your time and help.

채택된 답변

KSSV
KSSV 2016년 10월 31일
p = {[1,2,3,6,8,10]
[1,2,4,6,8,10]
[1,2,3,6,8,10,11]
[1,2,4,6,8,10,11]
[1,2,4,12]
[1,2,3,6,8,10,13]
[1,2,3,6,8,10,11,13]
[1,2,3,6,8,10,14]
[1,2,4,6,8,10,14]
[1,2,3,6,15]
[1,2,3,6,8,15]
[2,1]
[2,3]
[2,4]
[2,5]
[2,4,5]
[2,3,6]};
%
k = [] ;
for i = 1:length(p)
for j = 1:length(p)
if i ~= j
if all(ismember(p{i},p{j})) ;
% p(i) = [] ;
k = [k i];
break
end
end
end
end
pos = 1:length(p) ;
idx = setdiff(pos,k) ;
iwant = p(idx) ;
  댓글 수: 7
KSSV
KSSV 2016년 10월 31일
This shall work:
k = [] ;
for i = 1:length(p)
for j = 1:length(p)
if i ~= j
[temp1,temp2] = (ismember(p{i},p{j})) ;
if diff(temp2)==1
k = [k i];
break
end
end
end
end
pos = 1:length(p) ;
idx = setdiff(pos,k) ;
iwant = p(idx) ;
Note that [2,3,6] is there in the first path. So this is not recognized. It should be eliminated right?
Hari
Hari 2016년 10월 31일
Yes. It works. Thanks alot :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by