Skip to main content
13 votes
Accepted

Generating a list of integers that sums to zero

...
Roman's user avatar
  • 47.9k
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
  • 47.9k
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
  • 25.8k
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
  • 25.8k
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
  • 28.9k
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
  • 131k
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
  • 74.8k
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
  • 9,928
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,289
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
  • 74.8k
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
  • 396k
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
  • 396k
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
  • 52.6k
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
  • 4,907
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
  • 159k
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,948
4 votes
Accepted

Duplicate Tuples

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

What is the identity for Tuples?

Use TagSetDelayed to define a function that behaves as desired: ...
kglr's user avatar
  • 396k
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
  • 396k
4 votes
Accepted

Tuples which treats re-ordering as equivalent

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

Working with tables: add new level of nested tables

One possible implementation could be: ...
Syed's user avatar
  • 55.3k
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
  • 23.7k
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
  • 396k
3 votes

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

ClearAll[f0] f0 = Module[{ss = MapIndexed[Thread[{#2[[1]], #}] &, Subsets[Range@#, {#2}]]}, SparseArray[Join @@ ss -> 1, {Length@ss, #}]] &; ...
kglr's user avatar
  • 396k
3 votes
Accepted

Duplicate Tuples in two different ways

Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and ...
Lukas Lang's user avatar
  • 34.1k

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