Questions tagged [conditional]

Questions about programming or mathematical constructs that enable verification, selection, or branching behavior when one or more criteria are determined to be True.

Programming and mathematical conditional constructs including rules, patterns, and definitions used in pattern matching or pattern constraints.

See also: Conditioned, ConditionalExpression

523 questions
10
votes
1 answer

Multiple lines in an If statement

What I am wondering is how do you perform multiple lines when using the If statement. I know the following is not proper Mathematica code, but how would I do this in Mathematica? if(x<1, y=2x; z=2y else y=x/2; z=y/2 ) Mr. Wizard help: It does…
David
  • 14,883
  • 4
  • 44
  • 117
5
votes
3 answers

How to kick out points that are specific to a function

I have a list of xi,yi points. I want to remove any points from the list where x^2+y^2<1. I probably have to write a for loop of some sorts, not sure how to begin. Subscript[x, i] = -2 + 0.2 m; m = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,…
subrinarafiq
  • 165
  • 5
4
votes
1 answer

Confused about If statement

I have this simple test code: n1 = 0; n2 = 0; m = 0; If[n1 == 0, n2 == 1] which evaluates as False, as it should. However, if I do: If[n1 == 0, n2 == 1; m = 1]; m I get 1 instead of 0 (m value shouldn't change, if the expression evaluates to…
Silviu
  • 145
  • 4
4
votes
6 answers

How to avoid applying Cross to a single argument?

I am looking for the simplest way to avoid applying Cross to a single argument. PrimeFactorization[x_] := Cross @@ (Superscript @@@ FactorInteger[x]); Table[{n, PrimeFactorization[n]}, {n, 200, 250}] // TableForm
Friendly Ghost
  • 337
  • 2
  • 7
3
votes
1 answer

Fall through multiple cases in switch to execute same function

In C++ / C#, one can fall through multiple cases to execute one function instead of calling that function after each case. For example, switch (value) { case 1: case 3: case 4: DoExercise(value); break; case 2: …
Leonx
  • 413
  • 2
  • 9
3
votes
1 answer

Adapt elements in the list, condition by condition

Could you give me some advice? Suppose I have the following list. box[1,1]={{4.4, {1,1}, {a}}, {1.5, {1,1}, {a}}, {10.8, {2,1}, {3a}}, {10, {1, 3}, {3a}}} I want to do the following. When the location of Part[box[1, 1][[x]], 3] is {a}, apply it to…
hare
  • 406
  • 1
  • 8
3
votes
1 answer

Conditional list manipulation

I want to change a numerical list of the (examplary) form {1,3/2,5} to {1,{1,2},5}. First solution {3, 5/2, 1 } /. Rational[a_ , b_] -> {Floor[a /b], Ceiling[a/b ]} (*{3, {2, 3}, 1}*) works fine. Second try {3, 5/2, 1} /. (in_ /; (!…
Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
2
votes
2 answers

Nested If question

Using the following code, I got the first two piecewise equations right but the third one returns -99. Clear[x]; {PiecewiseExpand[Which[x < -1, -1, x < 1, x, True, 1]], PiecewiseExpand[If[x < -1, -1, If[x < 1, x, 1]]], PiecewiseExpand[If[x < -1,…
Richard
  • 49
  • 1
2
votes
2 answers

Shorter code for an If statement testing a variety of matches

How can I shorten these two If expressions into one variable="hello"; If[variable=="hi", Print["hi there"]]; If[variable=="hello", Print["hi there"]]; Longhand shortening: If[variable=="hello" || variable=="hi", Print["hi there"]]; to…
Tom Mozdzen
  • 521
  • 5
  • 16
2
votes
1 answer

Logical operations with defined conditions

I'd like to know how to define logical conditions to manipulate them. For example, say I have a list list of pairs of numbers, and I want to Select those which satisfy some condition cond = #[[1]] > 0 &; Then I just have to evaluate Select[list,…
MBolin
  • 257
  • 1
  • 6
2
votes
3 answers

One-line code to copy a list under numerical conditions

I want to know the simplest way to write this conditional copy function. I have 2 arrays of the same size: a={0,1,2,3,4} b={0,0,0,0,0} I want b to copy a's element, if that element is bigger than 2. expected result is : …
user1470393
  • 279
  • 1
  • 8
1
vote
1 answer

Problem with If statement

It is a very simple problem but I am confused. A comment is required. I have the following list masters={C18[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0], C18[1, 1, 1, 1, 1, 1, 0, 1, 1, 2, 0, 0]} Now if I write If [C18[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0]…
Tanmoy Pati
  • 345
  • 1
  • 7
1
vote
1 answer

If statement doesn't work properly

As the image shows, If doesn't return "Fail" if the condition didn't succeed, but works when conditions match. Why did that happen and how can I fix it? image:
David
  • 93
  • 5
1
vote
0 answers

How to generate conditionals programmatically?

I need to generate conditionals (||, &&, etc) from a list conditionList that I don't know beforehand how many/which elements will it contain. For simplicity, let's consider integers: conditionList = {3,5}; So, taking this list, I'd like to generate…
TumbiSapichu
  • 1,603
  • 8
  • 17
1
vote
2 answers

How do I loop my "If" statement?

I have list given by: d= {{{1,2},{1,1},{2,3}}, {{1,4},{3,3},{2,1}}, {{1,1},{3,1},{3,3}}} Each row in the list is a different generation of the list. I'm trying to figure out how to look at each iteration and see if any of the…
D'Angelo
  • 473
  • 2
  • 5
1
2 3