0
$\begingroup$

If I have a conditional of the form:

Which[Round[b[i], 0.1] == 74,1,40 < b[i] < x,2] where b[i] represents a number that gets updated in a loop and x is the variable I am asking for below.

How can I save the value of Round[b[i], 0.1] == 74 as a variable (let's say x as in the code) only for when the condition Round[b[i], 0.1] == 74 is met. So, to be more specific if b[i]=73.99, for example, then the code would translate to Which[Round[73.99, 0.1] (*which is equal to 74*) == 74,1,40 < b[i] < 73.99,2] but if b[i]=75, for example, then it will keep being Which[Round[75, 0.1] == 74,1,40 < b[i] < x,2] as Round[b[i]] is still not 74 and hence x is still a variable with nothing assigned to. Is this possible?

Edit: I will also appreciate comments on getting Round[b[i], 0.1] == 74 equal to a variable even when the condition is not met, which should be simpler to do. For this case I tried naively Which[x==Round[b[i], 0.1] == 74,1,40 < b[i] < x,2] but here x will be "True" or "False" but not the number.

I hope this makes sense and I appreciate in advanced your help

Thank you!

$\endgroup$
2
  • $\begingroup$ why can't you just do x = Round[73.99, 0.1]; If[x == 74, .etc....] ? $\endgroup$
    – Nasser
    Commented May 7, 2020 at 23:31
  • $\begingroup$ What are you expecting the Which to evaluate to when the x variable is unassigned? This feels like an XY problem.. $\endgroup$
    – Edmund
    Commented May 7, 2020 at 23:46

2 Answers 2

0
$\begingroup$

If you are already using a Which statement to control the flow, you can do more than one thing at a time by using ;. While it might seem like the purpose of ; is just to suppress output, it's actually the infix form of CompoundExpression which lets you join many expressions together where only one overall expression is allowed.

Which[
  Round[b[i], 0.1] == 74,
  x = Round[b[i], 0.1]; 1, (* you could also just put x = 74 *)
  40 < b[i] < x,
  2
]

You could also use brackets to set the assignment apart from the comparison. I think this would allow you to complete both tasks that you wanted:

Which[
  (x = Round[b[i], 0.1]) == 74,
  1,
  40 < b[i] < x,
  2
]

This is pretty close to what you had in your edit, I think you just needed the brackets.

$\endgroup$
2
  • $\begingroup$ Thank you @MassDefect your first code using ; (which I did not know was possible) is exactly what I wanted! $\endgroup$
    – John
    Commented May 8, 2020 at 0:42
  • $\begingroup$ @John No problem! Note that in the first code, x only gets an assignment if the first condition is met, while in the second code x always gets assigned regardless of which condition is met. $\endgroup$
    – MassDefect
    Commented May 8, 2020 at 0:52
1
$\begingroup$

Do you mean:

Which[
    If[Round[b[i], 0.1] == 74, x = Round[b[i], 0.1]]; Round[b[i], 0.1] == 74, 1,
    40 < bi < x, 2  
 ]

You can remove the value of x by using x=..

$\endgroup$

Your Answer

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

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