• 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

56 Solutions

7 Solvers

Last Solution submitted on Jul 29, 2026

Last 200 Solutions

Problem Comments

Solution Comments

Show comments
Loading...

Problem Recent Solvers7

Suggested Problems

More from this Author174

Problem Tags

Community Treasure Hunt

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

Start Hunting!