Problem 45303. Combinatorics - 01
- Input=[x,n]
- where x is an array of numbers(or strings) and n is a +ve number.
for example, x=[1,2] and n=6.
The output will be a matrix containing all the possible permutations of the vector x having n elements(no of columns=n). besides that -
- each row should contain equal number of occurance of the elements of x. (3 ones, 3 twos)
- no initial segment can have more 2's than 1's. e.g. [2 2 1 2 1 1] - is invalid.since there are more 2's in the 1st appearance than 1's.
- y=[
1 1 1 2 2 2 1 1 2 1 2 2 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 1 2 1 2 1 2 2 1 2 1 1 1 2 2 2 1 1 2 1 2 2 1 1 2 2 1 2 1 2 1 1 2 2 1 2 1 2 1 2 1 2 2 1 1 2 2 1 1 1 2 2 2 1 1 2 1 2 2 2 1 1 1]
Solution Stats
Problem Comments
-
4 Comments
I don't understand, can you explain the second rule?
all combinations of x & y of length n
1. they must appear n/2 times each
2. no initial segment of the string has more y's than x's
so, 'yyxyxx' -- the 2nd rule is broken here. since 1st appearance of x contains only one x while y [the second element] contains 2.
count (1stelement) >= count (2nd element) >= count (3rd element) ..
Hope this makes it clear
@Asif, thank you! I see.
Two suggestions:
1. Please clarify the second requirement, and in particular, avoid the term "initial segment", which is confusing. What I think you mean is that in each row, the number of elements in the first block of x(2)'s must not exceed the number of elements in the first block of x(1)'s.
2. Since you are not asking for the rows of y to be returned in any specific order, please change your test suite to use sortrows(), or indicate in which order they should be returned.
Solution Comments
Show commentsProblem Recent Solvers5
Suggested Problems
-
Create a cell array out of a struct
1830 Solvers
-
122 Solvers
-
67 Solvers
-
Longest run of consecutive numbers
5593 Solvers
-
939 Solvers
More from this Author165
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!