Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2023.1.0 - English


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,
  • if no duplicate is detected in x, the computations are done as without "unique", that is faster.
  • x is first sorted in ascending direction, and y is built in ascending lexicographic order.
Otherwise : The initial order of 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 2345678 910111213
y [bytes] 32144768480034560288240 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') — Random numbers
  • factorial — factorial function : product of the n first positive integers
  • unique — extracts (and sorts) distinct elements, rows or columns of a matrix
  • gsort — sorts boolean, numerical and string arrays
  • try — beginning of try block in try-catch control instruction

History

VersionDescription
6.1.0 "unique" option introduced.
Report an issue
<< ones Matrix generation repmat >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon May 22 12:37:05 CEST 2023