NEXTPERMPOS

the next combination of values in specific positions (extension of PERMPOS)
239 Downloads
Updated 10 Oct 2012

View License

This function is an extension of PERMPOS, for large cases.
A = PERMPOS(M,N) returns a matrix A in which each row contains a unique
permutation of M trues and (N-M) falses over N columns.
When the first input to PERMPOS is a vector V with M elements, each row
of A contains the M values in order, but uniquely distributed over the N
columns of A. See PERMPOS for examples.

The number of rows increases quite rapidly with increasing M and N, in
which case the use of NEXTPERMPOS may be used to avoid memory issues.

CN = NEXTPERMPOS(C) returns a single permutation permutation. The input C
is a vector with N elements, having M non-zero (or true) elements. CN
contains the next permutation of these M values over N positions.
CN has the same size as C.

Examples:
nextpermpos([1 0 0 1 0]) % -> [1 0 0 0 1]
nextpermpos([1 0 1 1 1 0 1]) % -> [1 0 1 1 0 1 1]
nextpermpos([99 0 23]) % -> [0 99 23]
isequal(nextpermpos([1 zeros(1,999) 2 0]), [1 zeros(1,1000) 2])

A = permpos(3,6)
for k = 1:size(A,1)-1
C1 = A(k,:) ;
CN = nextpermpos(C1) ;
if ~isequal(A(k+1,:),CN), disp('This should not happen.') ; end
disp(CN) ;
end

[C2, TF] = NEXTPERMPOS(C) returns a flag that is true if C2 is really the
next permutation. If C is the last possible permutation, C2 will be the
first permutation. Examples:
[c2, tf] = nextpermpos ([0 1 0]) % c2 = [0 0 1], tf = true
[c3, tf] = nextpermpos (c2) % c3 = [1 0 0], tf = false

See also permpos (matlab File Exchange), nchoosek

Cite As

Jos (10584) (2024). NEXTPERMPOS (https://www.mathworks.com/matlabcentral/fileexchange/38551-nextpermpos), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Arithmetic Operations in Help Center and MATLAB Answers
Acknowledgements

Inspired by: PERMPOS

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0