NEXTPERMPOS

版本 1.0.0.0 (2.4 KB) 作者: Jos (10584)
the next combination of values in specific positions (extension of PERMPOS)
240.0 次下载
更新时间 2012/10/10

查看许可证

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

引用格式

Jos (10584) (2025). NEXTPERMPOS (https://www.mathworks.com/matlabcentral/fileexchange/38551-nextpermpos), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2011b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Arithmetic Operations 的更多信息
致谢

参考作品: PERMPOS

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0.0