Linked Questions

207 votes
3 answers
55k views

Alternatives to procedural loops and iterating over lists in Mathematica

While there are some cases where a For loop might be reasonable, it's a general mantra – one I subscribe to myself – that "if you are using a ...
Mr.Wizard's user avatar
  • 273k
84 votes
4 answers
7k views

Using a PatternTest versus a Condition for pattern matching

My last question to the site resulted in several answers that involve using pattern matching in Mathematica, a feature I wasn't very familiar with at the time. I am currently reading Mathematica ...
Alfred Fazio's user avatar
  • 1,175
37 votes
7 answers
4k views

Are there "All" and "Any" functions in Mathematica?

In Python, there is a function all which returns true if all of its arguments are true, and any which returns true if at least ...
David Z's user avatar
  • 4,961
45 votes
5 answers
10k views

Fastest way to calculate matrix of pairwise distances

It is a very common problem that given a distance function $d(p_1,p_2)$ and a set of points pts, we need to construct a matrix ...
Szabolcs's user avatar
  • 236k
8 votes
4 answers
1k views

How to draw a hanging rootogram in Mathematica?

I am trying to plot a hanging rootogram of some data in Mathematica. I can't seem to find a built in function for it, while simply using Histogram (on "transformed" ...
Emayla's user avatar
  • 91
7 votes
3 answers
5k views

Efficient way to test if all the elements in a list are Integers? [closed]

Consider a list such as s = {1, 2, 3, 3, 5, 6, 3} With IntegerQ[number] , you know if ...
Mika Ike's user avatar
  • 3,261
5 votes
6 answers
794 views

Selecting sublists of different length if at least one element of the sublist fulfills a criterion

I have the following list, containing sublists with 1,2 or 3 elements. ...
Niki's user avatar
  • 960
6 votes
5 answers
683 views

Check whether array has only constant entries?

I have an array arr with n entries. Now I want to check whether ...
Mario Krenn's user avatar
  • 2,192
9 votes
3 answers
781 views

Generalization to AllTrue, AnyTrue and NoneTrue

I am wondering if there is a natural Mathematica way to generalize those functions. To be specific, All three functions AllTrue, ...
Sungmin's user avatar
  • 2,305
6 votes
6 answers
775 views

How do I replace elements in a list matching a pattern?

I have a list: {0, 1, 1, 1, 2, 2, 2, 6}. I want to replace all the elements that are greater than 1 with the integer 1. In other words, I want Mma to return the list {0,1,1,1,1,1,1,1,1}.
geoffrey's user avatar
  • 877
1 vote
2 answers
161 views

List manipulation - End loop

Given are two matrices (a & b). I want to end the For-loop if all values in the matrix (a minus b) are smaller than 7, in contrast to any value in the matrix (a minus b) is smaller than 7. Can ...
Paus's user avatar
  • 11
2 votes
2 answers
189 views

How to use Contains functions on matrices?

I need a quick test to check if a large matrix contains any non-zeros. The Contains functions work on Lists not matrices. ...
Jerry Guern's user avatar
  • 4,642
2 votes
1 answer
102 views

Type declaration for list arguments

How can I make f[n_Integer]=2*n valid for a list of integers? For example: f[nlist_Integer] := 2*nlist[[1]] does not work ...
anderstood's user avatar
  • 14.5k