# Tag Info

### Difference between Piecewise, Which and If?

The main difference is their intended use. Which is a "programming function". It is meant for flow control. It is an equivalent of ...
• 236k
Accepted

### How to define conditional function with Mathematica?

I think it's easier just to define this straight up, rather than compute something procedurally. f[1, 0] = 77; f[0, 1] = 66; f[_, _] = 0; Mathematica is ...
• 13.9k

### Flatten not working

f[...] := Module[{...}, ... /; condition] is a very special syntax (documented in the Details section of Module). It only works ...
• 236k

### Arrange a list in ascending order by deleting list elements

You can use DeleteDuplicates with Greater as the second argument: ...
• 399k
Accepted

### Can one compile the Switch function?

A drawback of the Which command is that it evaluates x repeatedly until one of the conditions of Which is satisfied. This is time consuming when x is a complicated expression or/and when the set of ...
• 236k
Accepted

### How to optimize the following function?

Using Ramp and vectorized operations: y = -.02 Range[0, 4 10^5]; Total @ Sqrt[Ramp[1 - 2 Exp[y]]] //AbsoluteTiming {0....
• 131k
Accepted

### How to restrict the domain of a function

I think the easiest way is to use Condition which has the operator form /;. ...
• 108k
Accepted

### Checking what a function depends on

Perhaps InternalDependsOnQ: h = f[x, g[y]]; InternalDependsOnQ[h, #] & /@ {x, y, g[y], w} {True, True, True, False}
• 399k
Accepted

### Generate "nice" random matrix

You can create random matrices until the eigenvalues do not have a Root form: ...
• 131k
Accepted

### conditional probability of dependent random variables

As @BobHanlon points out that the probability is zero for a specific value. But probabilities are not (necessarily) 0 in intervals. So we can get a probability statement for an interval and then ...
• 42.2k
Accepted

### Using "If" statement to change a list values

Use MapThread MapThread[If[# < 60, #2 + 20, #2] &, {Sample, Sample2}] or alternatively ...
• 20.4k

### How to avoid applying Cross to a single argument?

If it is only for displaying purposes you can use Row: ...
• 137k

### Flatten not working

Put Flatten inside Module: ...
• 399k
Accepted

### Take elements from a list based on two criteria

Select[list, Count[#, 1] == 1&] {{-1, 1}, {1, -1}, {1, (1 + I)/Sqrt[2]}} Also ...
• 399k

### Need four random values in a range which total unity

Because you have very tight constraints, the number of allowed points is not very large, so you can generate all of them and then sample. ...
• 29.3k
Accepted

### Can someone tell me what's wrong with this nested IF?

Change it to === and not == (examples at end) ...
• 148k
Accepted

### DeleteCases does not work with EvenQ? While Select works fine

For the selection criterion, Select uses a (pure) function, while *Cases uses a pattern. Code below works. ...
• 9,863

### Make a list of the first 100 primes, keeping only ones whose last digit is less than 3

SolveValues[x <= Prime[100] && Mod[x, 10] < 3, x, Primes] ...
• 399k
Accepted

Translation to make it easier to see. When you write If[n1 == 0, n2 == 1] This translates to ...
• 148k

### 2D random walk within a bounded area

You can use RegionWithin in Mathematica $11.1$ ...
• 8,988
Accepted

### Better use of pattern matching in replacing elements of a list

Given: list = {-3., -2.6, -2.2, -1.8, -1.4, -1., -0.6, -0.2, 0.2, 0.6, 1., 1.4, 1.8, 2.2, 2.6, 3.}; {min, max} = {-1.5, 3}; {pos, non} = {50, 700}; I'd just ...
• 69.1k

### How to select points from a table that are within some region

You should parametrize your region. c looks to me like sort of a radius and it will help to cut off half a plane. ...
• 73.4k
Accepted

### ContourPlot with certain contour values omitted

You can use the option RegionFunction: ...
• 399k
Accepted

### Finding the condition for root of a third degree polynomial

Assuming real coefficients and a=1, following Daniel Lichtblau comment, conditions are quickly found by: ...

### How to create a sublist of elements with a congruence relation

You can write a very explicit and readable code by using Select and AllTrue ...
• 2,105

### How to create a sublist of elements with a congruence relation

Indeed, Pick is very fast. A bit faster is using some integer vector arithmetic to create a scalar condition for picking: ...
Accepted

...
• 9,498

### Generate "nice" random matrix

I'll present another method to generate "nice" random matrices. I make no claims on the distribution followed by the matrices from my procedure. This method has three steps: use ...
Accepted

### How to avoid applying Cross to a single argument?

Since you already use Cross in a way that it's not meant to be used, you can also redefine it and assign a meaning to it when it has only a single argument: ...