PERMPOS

Version 3.0.1 (2.68 KB) by Jos (10584)
all possible ordered permutations of M values in N positions
3.7K Downloads
Updated 14 May 2019

View License

permpos(M, N) where M and N are non-negative integers produces a
logical (N!/M!(N-M)!)-by-N matrix in which each row contains a unique
permutation of M trues and (N-M) falses.

W = permpos(V, N) where V is an array with M elements a matrix W
where each row a contains the values of V in preserved order, but
uniquely permuted at the N columns. The remaining positions are set to
NaN. W = permpos(V, N, F) uses the value F rather than NaN.

[W, B] = permpos(V, ...) also returns the logical matrix B, which is
true for the positions that are filled by elements of V. Note that B
equals "permpos(numel(V),N)".

V can be a numerical array, a cell array of chars or a string
array. F (optional) should be of the same class as V.

Examples:
permpos(2, 4)
% -> [ 1 1 0 0
% 1 0 1 0
% 1 0 0 1
% 0 1 1 0
% 0 1 0 1
% 0 0 1 1 ]

permpos(1:2:3, 4) % ->
% -> [ 1 2 3 NaN
% 1 2 NaN 3
% 1 NaN 3 3
% NaN 1 2 3 ]

permpos({'a','b'}, 3, 'XX') % filler
% -> { 'a' 'b' 'XX' ; 'a' 'XX' 'b' ; 'XX' 'a' 'b'}

See also nchoosek, perms, randperm, true, false
On the file Exchange:
permn, PERMNK, permsk, allcomb, ballatsq, nones, nextpermpos

Cite As

Jos (10584) (2024). PERMPOS (https://www.mathworks.com/matlabcentral/fileexchange/11216-permpos), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2018b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers
Acknowledgements

Inspired: NEXTPERMPOS

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
3.0.1

new image

3.0

Implemented 2nd output and functionality for non-numeric inputs

2.4.0.0

small update

1.0.0.0

remove unnecessary check for input size (after review by Dmitri S.)