1
$\begingroup$

I would like to simplify:

Tableinit = {{"a", "b"}};
c = {{1, 2}, {1, 1}, {2, 1}};
Do[Print[set = {RandomReal[{0, 3}], RandomReal[{0, 3}]}]; 
 testP = {a -> set[[1]], b -> set[[2]]};
 If[And @@ 
   Thread[(a > c[[1, 1]] /. testP) && (b > c[[1, 2]] /. 
       testP) && (a > c[[2, 1]] /. testP) && (b > c[[2, 2]] /. 
       testP) && (a > c[[3, 1]] /. testP) && (b > c[[3, 2]] /. 
       testP)], Print["yaha"]], {3}]

So that I get for example:

{0.0135703,0.225061}
{1.34055,2.56798}
{2.74278,2.92141}
yaha

Into something more like this with an implied loop:

Tableinit = {{"a", "b"}};
c = {{1, 2}, {1, 1}, {2, 1}};
Do[Print[set = {RandomReal[{0, 2}], RandomReal[{0, 2}]}]; 
 testP = {a -> set[[1]], b -> set[[2]]};
 If[And @@ Thread[(a > c[[1]] /. testP) && (b > c[[2]] /. testP)], 
  Print["yaha"]], {3}] 

But this doesn't work. What am I doing wrong? Any help appreciated! Thanks.

$\endgroup$
1
$\begingroup$

Rather than a > c[[1]] you need something like Thread[a > c[[All, 1]]] or Thread[{a, b} > c[[1]]], e.g.

c = {{1, 2}, {1, 1}, {2, 1}};

Do[Print[set = {RandomReal[{0, 3}], RandomReal[{0, 3}]}];
 testP = {a -> set[[1]], b -> set[[2]]};
 If[And @@ Flatten[Thread /@ {a > c[[All, 1]], b > c[[All, 2]]} /. testP],
  Print["yaha"]], {3}]

or

Do[Print[set = {RandomReal[{0, 3}], RandomReal[{0, 3}]}];
 testP = {a -> set[[1]], b -> set[[2]]};
 If[And @@ Flatten[Thread[{a, b} > # /. testP] & /@ c],
  Print["yaha"]], {3}]

Comparison including two Mr.Wizard-style variations.

c = {{1, 2}, {3, 2}, {4, 1}};

Do[
 Print[set = {RandomReal[{0, 5}], RandomReal[{0, 4}]}];
 testP = {a -> set[[1]], b -> set[[2]]};

 If[(a > c[[1, 1]] /. testP) && (b > c[[1, 2]] /. testP) &&
   (a > c[[2, 1]] /. testP) && (b > c[[2, 2]] /. testP) &&
   (a > c[[3, 1]] /. testP) && (b > c[[3, 2]] /. testP),
  Print["yaha"]];

 If[And @@ Flatten[Thread /@ {a > c[[All, 1]], b > c[[All, 2]]} /. testP],
  Print["yaha"]];

 If[And @@ Flatten[Thread[{a, b} > # /. testP] & /@ c],
  Print["yaha"]];

 If[And @@ Thread[{a, b} > Max /@ Transpose@c /. testP],
  Print["yaha"]];

 If[AllTrue[c, And @@ Thread[# < {a, b}] &] /. testP,
  Print["yaha"]],
 {8}]
{4.74172,3.22551}
yaha
yaha
yaha
yaha
yaha
{4.00698,1.34669}
{3.91794,0.437822}
{1.33563,2.52507}
{4.56379,3.27889}
yaha
yaha
yaha
yaha
yaha
{2.30426,3.34091}
{0.417722,0.19335}
{1.05824,1.34333}
$\endgroup$
2
$\begingroup$

I was trying to preserve some semblance of your operations but I realize that just makes things confusing for this simple case. If you update your question to something where this doesn't work I'll try to address that too.

c = {{1, 2}, {1, 1}, {2, 1}};

Do[
 Print[set = RandomReal[{0, 3}, 2]];
 If[Max[c] < Min[set], Print["yaha"]],
 {3}
]

Attempting to make this more general I suggest you look at AllTrue, e.g.

AllTrue[c, # < Min[set] &, 2]

Or a bit more efficiently if set is longer:

AllTrue[c, # < m &, 2] /. m -> Min[set]
$\endgroup$
  • $\begingroup$ Sorry I didn't set up the best example because indeed it could be solved here with min and max, which is not the case in my operations but Thanks! $\endgroup$ – Elsa Sep 28 '17 at 15:35
  • $\begingroup$ @user52518 Did you attempt to apply AllTrue to your problem? I suspect it is applicable as you seem to want to test all elements of c. $\endgroup$ – Mr.Wizard Sep 28 '17 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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