659
edits
Carandraug (talk | contribs) (new section for combinatorics with problem permutations with repetition) |
|||
Line 141: | Line 141: | ||
These same tricks are useful for reading and writing data files with unique names, etc. | These same tricks are useful for reading and writing data files with unique names, etc. | ||
== Combinatorics == | |||
=== Permutations with repetition === | |||
==== Problem ==== | |||
You want to generate all possible permutations of a vector with repetition. | |||
==== Solution ==== | |||
Use {{codeline|ndgrid}} | |||
{{Code||<syntaxhighlight lang="octave"> | |||
[x y z] = ndgrid ([1 2 3 4 5]); | |||
[x(:) y(:) z(:)] | |||
</syntaxhighlight>}} | |||
==== Discussion ==== | |||
It is possible to expand the code above and make it work for any length of permutations. | |||
{{Code||<syntaxhighlight lang="octave"> | |||
cart = nthargout ([1:n], @ndgrid, vector); | |||
combs = cell2mat (cellfun (@(c) c(:), cart, "UniformOutput", false)); | |||
</syntaxhighlight>}} | |||
== Mathematics == | == Mathematics == |