MAT = NPERMUTEK(N,K) returns all possible permutations of the elements taken from vector N of length K. This type of sampling is an ordered sample with replacement.
This is also known as the permutations with repetition.
MAT has size (length(N)^K)-by-K, where K must be a scalar.
[MAT, IDX] = NPERMUTEK(N,K) also returns IDX such that MAT = N(IDX).
If N is of class single, MAT and IDX will be also.
hi,
I would generate permutations (n, k) with repetition, but only those whose sum is equal to n
for example
I have 5 to give candy to 3 children
I want to generate permutations with repetition, but only those whose sum is exactly equal to 5.
0 0 5; 0 5 0; 5 0 0; 1 4 0; etc..
does anyone know how?
thanks in advance matt for its functions, worked great
hi,
I would generate permutations (n, k) with repetition, but only those whose sum is equal to n
for example
I have 5 to give candy to 3 children
I want to generate permutations with repetition, but only those whose sum is exactly equal to 5.
0 0 5; 0 5 0; 5 0 0; 1 4 0; etc..
does anyone know how?
thanks in advance matt for its functions, worked great
hi,
I would generate permutations (n, k) with repetition, but only those whose sum is equal to n
for example
I have 5 to give candy to 3 children
I want to generate permutations with repetition, but only those whose sum is exactly equal to 5.
0 0 5; 0 5 0; 5 0 0; 1 4 0; etc..
does anyone know how?
thanks in advance matt for its functions, worked great
Thanks Matt! Now I got it.
For small arrays, this function is faster than your COMBINATOR.
H1-Line, help, example, contact address, "see also", input checks, extensive comments, works correct and efficiently => nice for teaching and computing!
Jan,
With the single class we are ok up to 16,000,000 elements. Even finding the number taken two at a time would lead to an array of size 5.12e+014 elements. I think this is beyond the reasonable, even with 64-bit computing! The other problem is that cumsum isn't supported for the integer classes, at least in 2007b. If you look at my COMBINATOR file, you will see that I have included a MEX which performs cumsum on integer classes.
Thanks.
Why is the index IDX a SINGLE array, if N is a SINGLE? Isn't this an unnecessary limitation when working with large arrays on a 64-bit Matlab? UINT32 would accept larger arrays with using all 32 bits for adressing. And addressing with UINT32 might be faster than with SINGLEs (depending on Matlab version?!).
--- But of course: The number of permutations will explode at far smaller limits, I know.
Is it helpful to cast CHAR to DOUBLEs temporarily, or would it be better to store them in smaller INT16?
You cannot index the output of npermutek(1:5,5) into an array (nuc) of length 4! The index exceeds dimensions because the output of the above call to NPERMUTEK will have many '5's in it. You will get the same error by doing this:
nuc = 'ABCD'
R = nuc(5) % or nuc(6) or nuc(7), etc. nuc only has 4 elements!
This is not an NPERMUTEK problem, it a user misunderstanding the output problem.
------------------
I'm not entirely sure where the index is exceeding matrix dimensions.... if only matlab was more specific with error listings.
------------------
But maybe this is only happening on my machine... can someone try and let me know ???
numel is a built-in Matlab function which returns the Number of ELements in the argument. If your version of Matlab is too old to have numel, you may have to write your own. For vector inputs to npermutek, you can also replace numel with length.
I thought that by defining the last column of the Index matrix first, there would be no advantage to pre-allocation. I was wrong, live and learn. File updated.
Comment only
19 Jun 2006
Jos vdG
sorry .. didn't see the acknowledgement ...
Jos
Comment only
19 Jun 2006
Jos vdG
Did you know <combnk> also here on the FEX? (File # 7147).
Looks like that does exactly the same.
Comment only
19 Jun 2006
matt fig (author)
I noticed that I missed a comment line in the help... this is fixed.
Comment only
Updates
19 Jun 2006
Error in help.
20 Jun 2006
Faster, due to remedying obvious omission of preallocation!
02 Oct 2008
Added a picture of output.
10 Oct 2008
Added keyword.
07 May 2009
Cut execution time in half for single output calls. Added full support for single class.
11 May 2009
Fixed typos (Matlab needs spellchecker for guys like me.)
31 Aug 2012
MathWorks changed the behavior of the DIFF function with character arrays, resulting in an error for this file when called as in the second help example.