Consider some table
tabb = RandomInteger[{0, 4}, {5, 4}]
and a row
row = RandomInteger[{0, 4}, 4]
How to check if row
matches with at least one row of tabb
?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communitytabb = RandomInteger[{0, 4}, {5, 4}];
row = RandomInteger[{0, 4}, 4];
Well, it's simply
MemberQ[tabb, row]
returning False
or True
depending on randomness
Also
Intersection[tabb, {row}] != {}
Or
IntersectingQ[tabb, {row}]
Or
FreeQ[{}] @ UniqueElements[{tabb, {row}}]
SeedRandom[1];
tabb = RandomInteger[{0, 4}, {5, 4}]
row = {0, 3, 4, 4}
ContainsAny[tabb, {row}]
True
Another alternative is to use Union
:
table = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
row1 = {4, 5, 6};
row2 = {10, 11, 12};
SameQ[Union[#1, {#2}], #1] & @@ {table, row1}
(*True*)
SameQ[Union[#1, {#2}], #1] & @@ {table, row2}
(*False*)
KeyExistsQ[Counts[tabb],rowThere]
KeyExistsQ[Counts[tabb],roww]
(*
True
False
*)
Data/Lists
elements = {"a","k",1,"12","cdt","b","m","l","q",132,12.345,999,1000,Exp[x^2],
Sin[y]};
tabb = Table[Take[RandomSample[elements],4],50];
roww = Take[RandomSample[elements],4];
rowThere = tabb[[-1]];
Throw/Catch
Do
loop:
Catch[Do[If[ContainsAll[rowThere][tabb[[i]]],Throw[True]],{i,1,Length@tabb}];
False]
(* False *)
Catch[Do[If[ContainsAll[#][tabb[[i]]],Throw[True]],{i,1,Length@tabb}];
False]&/@{roww,rowThere}
(* {False,True} *)
Catch[Scan[If[ContainsAll[rowThere][#],Throw[True]]& , tabb];False]
Catch[Scan[If[ContainsAll[roww][#],Throw[True]]& , tabb];False]
(*
True
False
*)
SameQ[row,#]&/@tabb
screen shot !Mathematica graphics This shows second row matched. if you want this can be changed to give rows numbers that match also. $\endgroup$MemberQ[tabb, row]
$\endgroup$