perms
Generate the table of permutations of given elements
Syntax
y = perms(x) y = perms(x, "unique")
Arguments
- x
Row or column vector of any data type for which the size(), ==, and [] operators are defined, including cells.
- "unique"
Optional text flag, to compute and return the whole set of unique permutations, without any duplicate row. This option can be used only if
x
is sortable. That applies to boolean, integer, real number, and text data types.- y
Array of the type of x, with n=size(x,"*") columns. Each row provides a permutation. Without using the "unique" option,
y
returns n! rows. Otherwise, the number size(y,1)≤ n! of rows depends on the multiplicities of unique values of x.
Description
Given a vector x
of length n
,
perms(..)
computes the expected number of permutations,
and checks if there is enough available memory to compute and store them as the result.
If there is not enough memory, nothing else is done and an error is generated.
Otherwise, the permutations are computed and returned.
When "unique" is used,
x components is kept, and the order
of permutations in y takes it into account. Thus, if necessary,
x can be sorted according to the order chosen by the user,
before calling perms() . |
As a reminder, here is the weight n*n!*8 of y
in bytes, vs the length n of x ,
if x are decimal numbers and "unique" is not used: |
n | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
y [bytes] | 32 | 144 | 768 | 4800 | 34560 | 288240 | 2.58×106 | 26.1×106 | 290×106 | 3.51×109 | 46.0×109 | 648×109 |
Examples
x = [4, 7, 10]; y = perms(x) x = ["a" "b" "c" "d"]; y = perms(x)' c = {"bonjour", %pi, %t}; perms(c)
--> x = [4, 7, 10]; --> y = perms(x) y = 10. 7. 4. 10. 4. 7. 7. 10. 4. 7. 4. 10. 4. 10. 7. 4. 7. 10. --> x = ["a" "b" "c" "d"]; --> perms(x)' ans = !d d d d d d c c c c c c b b b b b b a a a a a a ! !c c b b a a d d b b a a d d c c a a d d c c b b ! !b a c a c b b a d a d b c a d a d c c b d b d c ! !a b a c b c a b a d b d a c a d c d b c b d c d ! --> c = {"bonjour", %pi, %t}; --> perms(c) ans = [1x1 boolean ] [1x1 constant] [1x1 string ] [1x1 boolean ] [1x1 string ] [1x1 constant] [1x1 constant] [1x1 boolean ] [1x1 string ] [1x1 constant] [1x1 string ] [1x1 boolean ] [1x1 string ] [1x1 boolean ] [1x1 constant] [1x1 string ] [1x1 constant] [1x1 boolean ]
With duplicate elements:
perms([1 0 0]) perms([1 0 0], "unique") perms([0 1 2 0], "unique")' p = perms([0 0 0 0 1 1 1 2 2 3], "unique"); size(p)
--> perms([1 0 0]) ans = 0. 0. 1. 0. 1. 0. 0. 0. 1. 0. 1. 0. 1. 0. 0. 1. 0. 0. --> perms([1 0 0], "unique") ans = 0. 0. 1. 0. 1. 0. 1. 0. 0. --> perms([0 1 2 0], "unique")' ans = 0. 0. 0. 0. 0. 0. 1. 1. 1. 2. 2. 2. 0. 0. 1. 1. 2. 2. 0. 0. 2. 0. 0. 1. 1. 2. 0. 2. 0. 1. 0. 2. 0. 0. 1. 0. 2. 1. 2. 0. 1. 0. 2. 0. 0. 1. 0. 0. --> p = perms([0 0 0 0 1 1 1 2 2 3], "unique"); --> size(p) ans = 12600. 10. // instead of 10! = 3628800 rows
See Also
- grand(x,'prm') — Generate random numbers
- factorial — factorial function : product of the n first positive integers
- unique — remover todos os componentes duplicados de um vetor ou uma matriz
- gsort — sorts boolean, numerical and string arrays
- try — início de um bloco try numa instrução de controle try-catch (significa "tentar...")
History
Versão | Descrição |
6.1.0 | "unique" option introduced. |
Report an issue | ||
<< ones | Elementary matrices | repmat >> |