Skip to main content
13 votes
Accepted

Generating a list of integers that sums to zero

...
Roman's user avatar
  • 49.8k
12 votes

generating tuples of ones and zeroes with a fixed number of ones

L[n_, m_] := Permutations@Array[Boole[# <= m] &, n] L[3, 2] (* {{1, 1, 0}, {1, 0, 1}, {0, 1, 1}} *)
Roman's user avatar
  • 49.8k
11 votes
Accepted

How do I get a list of all possible sums in a list nested list?

Total[Tuples@rn, {2}] should do. For cases where a very large number of tuples would be generated, ...
ciao's user avatar
  • 26k
11 votes

List of tuples without duplicates & repeated values

The accepted answer will quickly blow up with arguments of more than trivial sizes. For example, with vals = {10, 20, 5, a, b, c} and ...
ciao's user avatar
  • 26k
11 votes
Accepted

How to create all $1$'s vectors of length $n$ without using Tuples[]?

ArrayPad[IdentityMatrix[n], {{0, 1}, {0, 0}}, 1] Or PadRight[IdentityMatrix[n], {n + 1, n}, 1] Or ...
lericr's user avatar
  • 34.1k
7 votes

Tuples of elements from list excluding anything with repeated values

Just use Permutations: Permutations[lst] {{a, b, s, t}, {a, b, t, s}, {a, s, b, t}, {a, s, t, b}, {a, t, b, s}, {a, t, ...
Carl Woll's user avatar
  • 132k
7 votes

What is the identity for Tuples?

If {{1},{2},{3}} is fine, you can use Nothing: ...
AccidentalFourierTransform's user avatar
7 votes

generating tuples of ones and zeroes with a fixed number of ones

With[{n = 5, k = 2}, ReplacePart[ConstantArray[0, n], Thread[# -> 1]] & /@ Subsets[Range[n], {k}]] ...
cvgmt's user avatar
  • 84.1k
7 votes
Accepted

Faster searching for subsets with pairwise conditions

Might be faster to cull as you go. I compile the selection function (possibly it can be made faster). Also I set the history length to zero although that does not help too much because the ...
Daniel Lichtblau's user avatar
6 votes
Accepted

List of tuples without duplicates & repeated values

n = 2; vals = {0, 1}; Tuples[vals, {n}] // DeleteDuplicatesBy[#, Sort] & As the comment said ...
AsukaMinato's user avatar
  • 10.4k
6 votes

Generating representatives of rotation classes of tuples of ones and zeros with a fixed number of ones

Not sure whether this works always correctly. Say you want the lost of all $n$-tuples with $k$ 1 up to rotation. You can encode such a tuple by going through it in ...
Henrik Schumacher's user avatar
6 votes

Faster searching for subsets with pairwise conditions

Correct me if I'm wrong, you're looking for 10,000 lists each with length 13, every element can be {1,2,3,4} and each list has at least 5 hamming distances from the ...
Ben Izd's user avatar
  • 9,549
6 votes
Accepted

Working with tables: add new level of nested tables

We can use FrobeniusSolve to solve the equation $$x_1+x_2+\cdots +x_n= m $$ Here n and m may ...
cvgmt's user avatar
  • 84.1k
6 votes

Tuples of digits with a given number of distinct elements

Subsets instead of Tuples: ...
Anton's user avatar
  • 1,188
5 votes

How do I get a list of all possible sums in a list nested list?

Few additional alternatives: Distribute[foo @@ rn, List, foo, List, Plus] Flatten @ Outer[Plus, ## & @@ rn] Activate @ Tuples[Inactive[Plus] @@ rn]
kglr's user avatar
  • 401k
5 votes
Accepted

How to list all possible 3-tuples with entries of the 3 tuples from 2 different sets?

{a, b} = {Range[4], Range[5, 8]}; triples = Tuples[{a, a, b}] ...
kglr's user avatar
  • 401k
5 votes
Accepted

Sorted Tuples without Filtering

The function you are looking for is: Subsets. E.g. for a list with n=5 elements and tuples with m=3 elements (note, you should not use capitalized variable names as those are used by the system): <...
Daniel Huber's user avatar
  • 56.4k
5 votes

How to create all $1$'s vectors of length $n$ without using Tuples[]?

You almost got the result you wanted, but somewhere you took a wrong turn. The question states The problem I have here, is that I want to concatenate this 1's vector with an identity matrix. The ...
Somos's user avatar
  • 5,065
4 votes

What is the identity for Tuples?

You can use Inactive[Sequence][] as identity like this: Tuples[{{Inactive[Sequence][]},{q}}]//Activate {{q}}
Thies Heidecke's user avatar
4 votes
Accepted

Generating a list of tuples that meet certain criteria without running out of memory

Clear["Global`*"] pool = Table[Range[3], 7] // Flatten; Rather than produce all tuples, produce a tuple on demand ...
Bob Hanlon's user avatar
  • 163k
4 votes
Accepted

Selecting from a list returned by Tuples

There was an excelent answer with an illustration, but I would like to point out the specific mistakes: Pure function in the second argmuent should end with & <...
Johu's user avatar
  • 4,968
4 votes
Accepted

Duplicate Tuples

Rather than use For loops, I suggest a route through Outer: ...
MarcoB's user avatar
  • 67.7k
4 votes

What is the identity for Tuples?

Use TagSetDelayed to define a function that behaves as desired: ...
kglr's user avatar
  • 401k
4 votes

generating tuples of ones and zeroes with a fixed number of ones

f[len_,wt_] := Table[ Boole[MemberQ[sub,i]], {sub,Subsets[Range[len],{wt}]}, {i,len} ] Try it online! ...
Greg Martin's user avatar
4 votes

generating tuples of ones and zeroes with a fixed number of ones

ClearAll[L2] L2[n_, m_] := Permute[PadRight[ConstantArray[1, m], n], SymmetricGroup @ n] L2[3, 2] ...
kglr's user avatar
  • 401k
4 votes
Accepted

Tuples which treats re-ordering as equivalent

You may use IntegerPartitions. ...
Edmund's user avatar
  • 43.2k
4 votes

Working with tables: add new level of nested tables

One possible implementation could be: ...
Syed's user avatar
  • 59.4k
4 votes

How to find repeating elements in the following grid?

If you know the lengths Length@grid1 == 6, Length@grid2 == 6, Length@grid3 == 5, then you ...
march's user avatar
  • 24.2k
4 votes
Accepted

How to find repeating elements in the following grid?

If we know that tuples is constructed from ngrids input grids and if each grid has at least one non-repeating element, we can ...
kglr's user avatar
  • 401k
4 votes
Accepted

Tuples of digits with a given number of distinct elements

...
ubpdqn's user avatar
  • 64.9k

Only top scored, non community-wiki answers of a minimum length are eligible