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.

Filter by
Sorted by
Tagged with
1
vote
1answer
30 views

Rewrite ListPlot using Graphics while perserving point color?

I have a lot of data points, and I have noticed that plotting using Graphics is usually much quicker than plotting with ListPlot. The problem is I am trying to color the data points based off some ...
3
votes
2answers
35 views

Replacement rules which for elements of a vector which meet conditions

I'm trying to create a function which will replace elements of a list with their values modulo 1, i.e. Mod[x,1], but only if ...
1
vote
1answer
51 views

How can I use WhenEvent with multiple conditions inside NDSolve?

I would like to use WhenEvent but I'm not getting what I want: I set the initial condition var\epsilon=0; I want var\epsilon to change continuously over time, from to 0.1 if s'[t]>=0 or to -0.1 if s'[...
1
vote
2answers
44 views

Replace the values of second position in a list with the values of another list given a condition

If I have the following two lists: list1= https://pastebin.com/zFin7kkB list2= https://pastebin.com/8ymCin6d where the first element of both list is the same. How can I replace the values in the ...
0
votes
1answer
28 views

Replace the values of the second position in a list given two conditions

Suppose I have the following list: https://pastebin.com/zFin7kkB and I have a given value of maxDh (let's say 0.5) . How can I replace only the values in the ...
1
vote
2answers
62 views

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 ...
4
votes
2answers
70 views

Using pure functions to assign a value to variable depending on whether it has been defined already

Let's say I want to assign to a variable the value $2020$ if it has not already been defined and do nothing if it has. In short, for a variable $a$, I want to achieve the functionality of ...
5
votes
2answers
1k views

Generate “nice” random matrix

I don't frequent this community, so please give criticism if this is a poor question. I am trying to make a tool which generates a small (say, $4\times 4$) matrix with integer entries, which has a "...
2
votes
1answer
25 views

Which condition inside a loop with unknown value in a condition

I have a Which condition inside a loop such as: ...
0
votes
2answers
25 views

Assign a variable to a condition only if the condition is met using Which

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 ...
0
votes
0answers
21 views

Dynamically fixing a Control within Manipulate

Consider the following code Manipulate[x, Control[{{y, 0, "y"}, {1, 0}}], Control[{{x, 0, "x"}, {1, 0}, Enabled -> Dynamic[y == 1]}]] where ...
1
vote
0answers
26 views

How to avoid delayed evaluation in Condition used in a function definition

I would like to define a function (below, f) that evaluates only under the condition that some expression is True. The condition is to be specified now, and I don't want it to change for the purposes ...
0
votes
0answers
17 views

Is probability perspective dependent? [migrated]

Suppose there be a person 'A' doing coin unbiased coin flips and he gets 4 heads in a row, then the chance of the next being a tail is 97%, which is very very likely. But if person 'B' unaware of 'A'...
3
votes
3answers
189 views

Inefficient NIntegrate and Which

In version 10 a simple numerical integraton of piecewise function is highly inefficient: ...
2
votes
2answers
78 views

Conditional statement in function

It seems the equality condition does not work as expected. ...
0
votes
0answers
46 views
0
votes
0answers
50 views

How do I correctly set up an “If” statement?

I have the following list of variables: z=1; a={{A,B,C,D},{A,A,E,W},{B,B,B,B},{S,R,B,T,D},{M,M,M,M}} What I'm trying to do is set up a statement that reads ...
1
vote
0answers
39 views

Meaning of a condition $\left(\Re\left(m^2\right)\geq 0\lor m^2\notin \mathbb{R}\right)$ [closed]

I'm new to using Mathematica. I asked Mathematica to perform the following integration, ...
3
votes
3answers
73 views

count conditional items in list

I have a list of the form: {1,1, (1 if x>y, -1 if y>=x), -1} I want to count the number of 1's in the list. The output I'm looking for is something of the ...
0
votes
0answers
21 views

Optimization of storages capacity

Consider a series of storages where different goods are added into daily. I would like to optimize the distribution of these goods between the storages in a way which takes into account some ...
0
votes
0answers
41 views

Integrals with conditional result

I am trying to get Mathematica to evaluate integrals such as the well-known Fourier orthogonality relations $\int_0^L \sin \frac{2 m \pi x}{L} \sin\frac{2 n \pi x}{L}\,\mathrm{d}x=\begin{cases}0& ...
3
votes
2answers
111 views

How to keep Switch default case as original function?

For example of below Switch function. In[]:= Module[{f}, f[x_] := Switch[x, 1, 2, 3, 4]; {f[1], f[3], f[x]} ] Out[]= {2, 4, Switch[x, 1, 2, 3, 4]} ...
2
votes
2answers
44 views

Vanishing function and Manipulate

Within Manipulate, I want to vanish certain controls if other controls satisfy a specific condition. The simplest case is to consider two checkboxes, ...
1
vote
1answer
44 views

Plot when multiple conditions are satified [closed]

I have a code which goes like this: ...
0
votes
1answer
51 views

Is using an If[] condition faster than KronckerDelta[]?

I have a code which has a term in a summation like: KroneckerDelta[M,0]*func[M] where func[M] is a time consuming numerical ...
1
vote
1answer
40 views

Defining a conditional statement for a function

I am trying to define a function that is zero when it is evaluated on real numbers and on a preset list of symbols. I am doing this by using ...
0
votes
0answers
11 views

Min of a set defined with conditions

I would like to define this parameter inside a function: $R = \min\left\lbrace i \in \left\lbrace 1,\cdots,n \right\rbrace \; : \; ((p+i)\mod n) \; \neq a_{j} \; \forall j \in \left\lbrace 1,\cdots,m ...
2
votes
3answers
111 views

Specifying condition with NIntegrate

I have an integral $$ P_{\frac12 + \mathrm i\mu}(\eta) = \frac{\sqrt{2}}{\pi} \cosh(\pi\mu) \int_{0}^{\infty} \frac{\cos(\mu\tau)}{\sqrt{\cosh\tau + \eta}} \mathrm d\tau, \quad \eta \geq 1, \quad \mu ...
4
votes
1answer
126 views

Why does D ignore conditions specified on a custom function?

I define a function with a condition like so. ClearAll[foo]; foo[expr_] /; FreeQ[expr, bar] := expr Such that foo[12] returns ...
5
votes
3answers
142 views

Among the first 10,000 multiples of 17

Hello I'm new with Mathematica and I can't find a way where I can do this condition. Among the first 10,000 multiples of 17 how many have the sum of their digit multiple of 17? The part for the ...
1
vote
0answers
29 views

Are the conditions in ConditionalExpression necessary or sufficient?

I computed a limit and obtained it in the form of a ConditionalExpression (see below). Substituting the variables with numerical values in the resulting formula ...
1
vote
0answers
58 views

What does a single vertical bar mean in ConditionalExpression? [closed]

I got the condition below as part of a ConditionalExpression. Those single vertical bars before and after $\frac{1}{k1}$ appear to be an absolute value but I don't ...
0
votes
0answers
45 views

Using If statement to evaluate lists

I have a list of x,y values and i am finding the distance between each point, So for 20 point in my file I have something like this, ...
4
votes
1answer
78 views

Use Lookup on associations that contain conditional statements

I am using conditional statements in a list of rules to replace values in a vector of data. Since the same operation can be performed faster using Lookup on associations I would like to use this ...
1
vote
1answer
68 views

Multiple conditions in If function

I wrote a function which returns 1 if x^2 + y^2<=1: f[x_, y_] := If[x^2 + y^2 <= 1, 1] Now I'm trying to continue this function to take in multiple ...
5
votes
4answers
297 views

Recursive function, if and which statements

I am given this question: Let a function $ T_n(x) $ be defined by: $$ T_{n+1}(x) = \frac{1}{x} T_{n-1}(x) - \frac{2}{7} T_n(x), $$ where $ T_0(x) = 1, T_1(x) = x $. I need to construct a recursive ...
1
vote
1answer
44 views
2
votes
2answers
112 views

Automate table to display figures

With reference to the post Automate Poisson Football Scores Prediction, I succeded in defining the Poisson probability density function for home (μh=A) and away (<...
1
vote
1answer
35 views

How to use conditional clause inside NDSolve

I am trying to solve a differential equation that changes its behaviour depending on the value of the dependent variable. I've tried as shown below, but the computer does not understand my If clause ...
3
votes
2answers
105 views

How does mathematica decide what `x_` is in `MemberQ[list,x_/;(x==#)]&/@list`

Consider the following code: list={1,2} MemberQ[list, x_ /; (x == #)] & /@ list This gives {True,True} ...
0
votes
0answers
22 views

Using a Which statement with Graphics3D [duplicate]

*Problem solved in the comments I am trying to write a function that outputs a different graphic depending on the user's input, with four total options. I was making a test version and am running ...
3
votes
3answers
131 views

Is there a way to speed up the following calculation Table[If[TrueQ[],k,Nothing],{k,10^5,10^7}]

I've the following code: Table[If[TrueQ[2+3^k>2^k+(-1+2^k)*Floor[(3/2)^k]],k,Nothing],{k,10^5,10^7}] Is there a way to speed the calculation up in Mathematica ...
1
vote
1answer
40 views

Aligning Conditional Controls in Manipulate

Consider the following code ...
13
votes
0answers
108 views

Reduce[Sin[x]==0,x] splitting odd and even multiples of Pi

as from Title, I'm wondering why Mathematica does not automatically give a single, simple solution for the problem Reduce[Sin[x] == 0, x] for which I obtain the ...
5
votes
2answers
661 views

How to optimize the following function?

Consider the following function: function[x_, y_] = If[x < Exp[y], Sqrt[1 - x/Exp[y]], 0]; functionTotal1[x_] = Sum[function[x, 0.02*j], {j, 0, 4*10^5, 1}]; ...
1
vote
1answer
60 views

Identifying transitions in an array

I have two arrays of elements. The second array is generated by the transfer of one or more element to another position. I want to know which element is transferred to which position. Following is the ...
7
votes
2answers
225 views

Randomly generated coordinates

I want to code that will do the following: Write a Mathematica function called randomHop that takes as its argument a list containing two coordinates x and y; ...
1
vote
2answers
51 views

How to choose model parameters using `Piecewise` function

The following code (developed by m_goldberg) works as expected. The model parameters can be selected by using Button, being ...
0
votes
1answer
51 views

Conditionally returning a constant if function returns Undefined

Often times functions might return Undefined in Mathematica, e.g. when the Volume@RegionIntersection is called for 2 objects ...

1
2 3 4 5
8