Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to detect if a set of pairs, for example a={{2,5},{6,3},{1,4},{8,7}} contains a given pair, for example tabu={{4,1},{2,6}} but don´t be relevant the order inside pair (pair {i,j} is equal to {j,i}), so in this case.... YES!!! "a has an element of tabu.

That´s the only target.

So, Are there any function to do this directly?

I was thinking in a variable Tabustatus=TRue if the big contains any pair of tabu, or tabustatus=False if not contais. Ordering previously the pairs for avoid the sort issue(no matter) any similar to:

a = {{1, 2}, {4, 3}, {3, 2}, {5, 2}};
tabu = {{2, 6}, {4, 1}};
asortg = Table[Sort[a[[i]], Greater], {i, 1, Length[a]}];
asortl = Table[Sort[a[[i]], Less], {i, 1, Length[a]}];
tabusortg = Table[Sort[tabu[[i]], Greater], {i, 1, Length[tabu]}];
tabusortl = Table[Sort[tabu[[i]], Less], {i, 1, Length[tabu]}];
Print["a...........", a];
Print["a - Greater.", asortg];
Print["a - Less....", asortl];
Print["tabu...........", tabu];
Print["tabu - Greater.", tabusortg];
Print["tabu - Less.....", tabusortl];
tabustatus = False;
Do[Do[Print["i..", i, "  j..", j];If[asortl[[i]] == tabusortl[[j]], Print[asortl[[i]], " - ", tabusortl[[j]]]; tabustatus = True; Break[], False]; If[tabustatus == True, Break[]], {i, 1, Length[asortl]}]; If[tabustatus == True, Break[]], {j, 1, Length[tabu]}];
Print["Tabu Status...", tabustatus];

In this case, I would need to add any Breaks[] to exit If... Do...Do... without continue searching,... since one time one is found,... TABUSTATUS=TRUE, We don´t need to continue searching.

Any faster and less tedious alternative?

share|improve this question
up vote 3 down vote accepted
Or @@ (MemberQ[Sort /@ tabu, #] & /@ Sort /@ a)

(* True *)

There are many ways to do this, above is fine for reasonable lists sizes, if your lists are large, there are faster methods.

share|improve this answer
Could you put the complete solution? because the other answer were delted. I think that There was an error, as you can see in – Mika Ike Apr 19 '14 at 9:24
@MikaIke: Not sure if you're commenting @ me, mine gives correct True for the data in that image. Other answer was not mine... – ciao Apr 19 '14 at 9:29
Yes, I only refered to whats before OR@@.... to be a function. For other visitors.The solution seems to be includes[list_, tabu_] := Or @@ (MemberQ[Sort /@ tabu, #] & /@ Sort /@ list) rasher said, and other colleague puts other deleted answer. The other was erroneus, I think , as you see in – Mika Ike Apr 19 '14 at 9:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.