1

I have a math problem about Mathematica, I am looking help from you. I hope that you could help me.

The Math Problem:

list= { {0,0,1},{1,0,0},{0,1,0} ,{1,2,3}};

as you know that list is a List,

and the first three elements of the list is list[[1]]、list[[2]]、list[[3]].

but the three elements of list[[1]]、list[[2]]、list[[3]] are all the same, that is 1,0,0.

the only difference is their order.

So I need a function to deal with the List:

The Results must been p={{0,0,1} ,{1,2,3}} (also can been p={{1,0,0} ,{1,2,3}},etc).

I know two functions can do that:

(1)

p = DeleteDuplicates [ list, Sort[#1] == Sort[#2] & ];

(2)

p = Union[list,  SameTest -> (SameQ[Sort[#1], Sort[#2]] &) ]

when List has 100,000 elements (Length[list]=100,000) , the speeds of these two functions

are too slow(It takes me a day, but has no results).

eg

list={};
For[i=1,i<=100000,i++,
list=Append[list,{0,0,Random[Integer,{1,5}]}]
];
p = DeleteDuplicates [ list, Sort[#1] == Sort[#2] & ];

Could you help me or give some suggestions ?

kglr
  • 394,356
  • 18
  • 477
  • 896
YuYong
  • 129
  • 6
  • Can you not simply sort the subsets?: DeleteDuplicates[Sort /@ set] – Mr.Wizard Jul 18 '14 at 12:39
  • No,Can not, eg, list= { {0,0,1},{1,0,0},{0,1,0} ,{3,2,1}};When I use sort the results is {{0,0,1},{1,2,3}},But I want that {3,2,1}'s elements order cannot change. (It is element's order in finite element, so cannot change order) – YuYong Jul 18 '14 at 12:46
  • I have marked this question as a duplicate of one that I believe is equivalent. Please take a look at it. (The link is now at the top of your question.) The solution is to use GatherBy rather than DeleteDuplicates with a custom comparator, so you want: First /@ GatherBy[set, Sort]. The reason why is explained in my answer there. – Mr.Wizard Jul 18 '14 at 12:49
  • Ok,Thanks, I will look your questions carefully, Thanks! – YuYong Jul 18 '14 at 12:52
  • The answer:list = {}; For[i = 1, i <= 40000, i++, list = Append[ list, {Random[Integer, {1, 5}], Random[Integer, {1, 5}], Random[Integer, {1, 5}]}] ]; First /@ GatherBy[list, Sort[#] &] // Timing // First DeleteDuplicates[list, Sort[#1] == Sort[#2] &] // Timing // First – YuYong Jul 18 '14 at 13:25
  • By the way, try to get away from using For and Append. Also, Random is deprecated; you should use RandomInteger, RandomReal etc. This is not just syntax; they produce higher quality pseudorandom numbers. Your list could be built with: list = RandomInteger[{1, 5}, {40000, 3}];. Faster, better, and a lot easier to write. :-) – Mr.Wizard Jul 18 '14 at 13:43
  • More links for you: (7924), (2930) – Mr.Wizard Jul 18 '14 at 13:46
  • @ Mr.Wizard,I have another similar question to Consult. list1 = RandomInteger[{1, 5}, {40000, 3}]; list2={1,2,3,4,5}; and I want to find that sublists of list1 that contain any element of list2? And I pastes the questions in the codes?The speed of the procedure is important. Thanks for your help... – YuYong Jul 21 '14 at 08:07
  • @Mr.Wizard and the main problem is problaly that the speed of Intersection is slow – YuYong Jul 21 '14 at 08:14
  • In your given example all subsets intersect because they only contain elements in the range {1, 5} which all belong to list2. What does your actual data look like? Also I think it would be better to post this (Question2) separately, along with the more realistic example data. For general use I doubt Intersection can be greatly improved upon, however for certain data there are interesting options such as bit-masks. – Mr.Wizard Jul 21 '14 at 08:20
  • @Mr.Wizard , I have pasted my problems again, Thanks for your help, It may some difficulty to understand all codes..But I have pointed that:list1=ElemElemsMadeofPtsIndexandIdex;

    list2=BoundaryPtsIndex;

    – YuYong Jul 21 '14 at 08:53
  • @Mr.Wizard, and I have to say that the procedure may only work in Mathematica v10.0.0 – YuYong Jul 21 '14 at 08:57
  • YuYong, you should split this Question and post the second part as a new Question. – Mr.Wizard Jul 21 '14 at 09:07
  • @Mr.Wizard,Ok,I did it http://mathematica.stackexchange.com/questions/55451/find-sublists-of-a-list-match-some-conditions – YuYong Jul 21 '14 at 10:59

0 Answers0