nsumk

バージョン 1.7.0.0 (1.67 KB) 作成者: Peter Cotton
Returns the number and listing of n-tuples of non-negative integers adding up to k.
ダウンロード: 1.1K
更新 2010/8/15

ライセンスの表示

Little more than syntactic sugar for nchoosek, this small but surprisingly controversial function returns the number of (ordered) n-tuples of non-negative integers adding up to k, and if supplied a second argument, a listing of them. As an alternative to downloading, just cut and paste the following:

m = nchoosek(k+n-1,n-1);
dividers = [zeros(m,1),nchoosek((1:(k+n-1))',n-1),ones(m,1)*(k+n)];
x = diff(dividers,1,2)-1;

It has been noted with some passion that this it is possible to achieve the same result using partitions.m, a more general function posted on Matlab Central. Indeed nsumk(n,k) returns the same result as sortrows(partitions(k, ones(1,n))). For small problems the latter is probably no more than 10x slower than using nchoosek. For large problems the performance of nsumk is substantially better (but only because we desire a special ordering).

These observations may be more relevant to others than this author, and no effort has been made to optimize nsumk (or for that matter nchoosek). See vchoosek.m for a faster .mex implementation of nchoosek.

引用

Peter Cotton (2024). nsumk (https://www.mathworks.com/matlabcentral/fileexchange/28340-nsumk), MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2010a
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
カテゴリ
Help Center および MATLAB AnswersNumeric Types についてさらに検索
謝辞

ヒントを得たファイル: Partitions of an integer, VChooseK, Unique random permutations

ヒントを与えたファイル: laplace.m

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.7.0.0

Included link to vchoosek.m

1.6.0.0

Modified comments

1.5.0.0

Modified documentation

1.4.0.0

Fixed upload error

1.3.0.0

Modified the extremely controversial overloading of the first argument

1.2.0.0

Forgot to include partitions.m (required for nsumkdaft.m)

1.1.0.0

I have included nsumkdaft.m which does the same thing using partitions.m

1.0.0.0