2
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ How about using SameQ? as in 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$
    – Nasser
    Sep 27 at 8:24
  • 4
    $\begingroup$ MemberQ[tabb, row] $\endgroup$
    – Domen
    Sep 27 at 8:25

4 Answers 4

5
$\begingroup$
tabb = 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}}]
$\endgroup$
3
$\begingroup$
SeedRandom[1];
tabb = RandomInteger[{0, 4}, {5, 4}]
row = {0, 3, 4, 4}

ContainsAny[tabb, {row}]

True

$\endgroup$
3
$\begingroup$

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*)
$\endgroup$
1
$\begingroup$
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} *) 

Scan

Catch[Scan[If[ContainsAll[rowThere][#],Throw[True]]& , tabb];False]
Catch[Scan[If[ContainsAll[roww][#],Throw[True]]& , tabb];False]

(* 
  True
  False
*) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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