perms
All permutations of the elements of a vector.
Category: Mathematics > Elementary Math
Syntax
P = perms(v)
Returns every ordering of the elements of v, one per row.
Description
The rows come out in reverse lexicographic order of the positions: the first row is v reversed and the last is v itself. The number of rows grows as n!, so perms(1:10) already has 3,628,800 rows and perms(1:12) is too large for memory.
Because repeated values count as different, perms([1 1 2]) has 6 rows with duplicates; apply unique(..., 'rows') to remove them.
Input arguments
v- Vector of numbers, characters or other values. Repeated values are treated as distinct.
Output arguments
P- Matrix with
factorial(numel(v))rows andnumel(v)columns, of the same class asv. A char vector gives a char matrix.
Example
Numbers and characters
perms([1 2 3])
perms('abc')
Output
ans = 6×3
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
ans = 6×3 char array
'cba'
'cab'
'bca'
'bac'
'acb'
'abc'