Main Content

perms

All possible permutations

Description

example

P = perms(v) returns a matrix containing all permutations of the elements of vector v. Each row of P contains a different permutation of the n elements in v. Matrix P has the same data type as v, and it has n! rows and n columns.

Examples

collapse all

v = [2 4 6];
P = perms(v)
P = 6×3

     6     4     2
     6     2     4
     4     6     2
     4     2     6
     2     6     4
     2     4     6

v = uint16([1023 4095 65535]);
P = perms(v)
P = 6x3 uint16 matrix

   65535    4095    1023
   65535    1023    4095
    4095   65535    1023
    4095    1023   65535
    1023   65535    4095
    1023    4095   65535

v = [1+1i 2+1i 3+1i];
P = perms(v)
P = 6×3 complex

   3.0000 + 1.0000i   2.0000 + 1.0000i   1.0000 + 1.0000i
   3.0000 + 1.0000i   1.0000 + 1.0000i   2.0000 + 1.0000i
   2.0000 + 1.0000i   3.0000 + 1.0000i   1.0000 + 1.0000i
   2.0000 + 1.0000i   1.0000 + 1.0000i   3.0000 + 1.0000i
   1.0000 + 1.0000i   3.0000 + 1.0000i   2.0000 + 1.0000i
   1.0000 + 1.0000i   2.0000 + 1.0000i   3.0000 + 1.0000i

Input Arguments

collapse all

Set of items, specified as a vector of numeric, logical, or char values.

Example: [1 2 3 4]

Example: [1+1i 2+1i 3+1i 4+1i]

Example: int16([1 2 3 4])

Example: ['abcd']

Example: [true false true false]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

Limitations

perms(v) is practical when length(v) is less than about 10.

More About

collapse all

Permutations

Permutations of a set are all possible arrangements of the elements of the set. P = perms(v) returns permutations of the elements of a vector v in reverse lexicographic order of the element indices in v. For example, for a vector v with 3 elements, P = perms(v) returns the same output as:

P = [v(3) v(2) v(1);
     v(3) v(1) v(2);
     v(2) v(3) v(1);
     v(2) v(1) v(3);
     v(1) v(3) v(2);
     v(1) v(2) v(3)]

Extended Capabilities

Version History

Introduced before R2006a