1
$\begingroup$

I am trying to create a conditional Table with If statements, where I move to the next condition only if the first one is satisfied, otherwise I "Break[]" and move to the next element of the table. For now, I have created a table with my conditions but I can't find a way to specify an order between these conditions.

As an example, let's take the function:

f[x,y]=(1-x y)/(x y-0.1)

with 0 < x < 1 and 0 < y < 1 and I want to get a 3x3 matrix looking at the values taken by f for x={0,0.5,1} and y={0,0.5,1}. So if I calculate f for these different values, I get this board of values:

enter image description here

I am interested only in knowing if the f[x,y] for each value pair is <0 or >0 and <2 or >2. In other words I want to get this output:

enter image description here

However the closest I found is:

f[x_, y_] = (1 - x y)/(x y - 0.1)
TableForm[Table[
If[f[x, y] <= 0, f < 0, ## &[]] && 
If[f[x, y] <= 2, 0 < f < 2, f > 2],
{x, 0, 1, 0.5}, {y, 0, 1, 0.5}]
, TableHeadings -> {{"", y, ""}, {"", x, ""}}]

enter image description here

where I am looking at the 2 conditions no matter what is the answer to the first one.

How can I say "If f<0, don't look at the second condition, move directly to the next element of the table (to avoid useless calculations); otherwise, don't print anything and look at the second condition. Then if f<2, print 0 < f < 2, otherwise print f > 2"?

I tried to add a Break[] in the first condition but it doesn't work...

Thank you so much in advance!

$\endgroup$
  • 1
    $\begingroup$ Give TableForm[ Table[Which[f[x, y] <= 0, "f<0", 0 < f[x, y] < 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}] a whirl (I did not correct the inconsistent test vs output, btw...) This is corrected, generates example: TableForm[ Table[Which[f[x, y] < 0, "f<0", 0 <= f[x, y] <= 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}] $\endgroup$ – ciao Sep 4 '15 at 0:35
  • $\begingroup$ Thanks! How do I specify to StackExchange that you've answered to my question? $\endgroup$ – Elsa Sep 4 '15 at 0:45
  • $\begingroup$ Well, since it's a comment, you can't. I'll post as an answer, then you can click the check-mark... $\endgroup$ – ciao Sep 4 '15 at 0:47
  • $\begingroup$ Thanks! and additional question: how can I get the value pairs {x,y}, which got True? (so here it would be {0.5,0.5}) $\endgroup$ – Elsa Sep 4 '15 at 2:52
1
$\begingroup$

Give

TableForm[ Table[Which[f[x, y] <= 0, "f<0", 0 < f[x, y] < 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}] 

a whirl (I did not correct the inconsistent test vs output, btw...) This is corrected, generates example:

 TableForm[ Table[Which[f[x, y] < 0, "f<0", 0 <= f[x, y] <= 2, "0<f<2", True, "f>2"], {x, 0, 1, 0.5}, {y, 0, 1, 0.5}], TableHeadings -> {{"", y, ""}, {"", x, ""}}]
$\endgroup$
1
$\begingroup$

Just for variety and exploiting same x and y range:

g[x_, y_] := {Boole[# < 0], Boole[0 <= # < 2]} &@
   f[x, y] /. {{0, 0} -> "f>=2", {1, 0} -> "f<0", {0, 1} -> "0<=f<2"}

then

With[{r = {0, 0.5, 1}}, 
 TableForm[Partition[g @@@ Tuples[r, {2}], 3], 
  TableHeadings -> {r, r}]]

enter image description here

$\endgroup$
  • $\begingroup$ Very interesting! Thank you, I got to learn about new functions! Also do you know how, with function "Which", can I get the value pairs {x,y}, which got True? (so here it would be {0.5,0.5} $\endgroup$ – Elsa Sep 4 '15 at 16:30

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.