Do you know any Group Theory ? The reason I ask is that the question you asked is most conveniently answered in the language of Group Theory . The short answer to your question is that MatLab doesn ' t have any nice facilities for dealing with the permutations so you ' d have to write all your own code or hope you can find some already written for you . The best program for doing what you want is system . org , but the learning curve may be steeper than you care for . groups has some facilities as does ~ deloeraTEACHINGMATH 146 introgrouptheory . pdf and but I haven ' t used either . A way to think of this is as a sort of factoring problem . You want to factor a random permutation in terms of your two generators s 1 and s 2 . That this is even possible is because s 1 and s 2 are known to generate the symmetric group S 4 . GAP has facilities for doing this for random groups , but I think you don ' t need it here as the group is small . As uDasStratTone mentions a simple way to answer this question is to construct the Cayley Graph using s 1 and s 2 as your generators , is the graph in question . By examining the graph , you will be able to determine all the factorizations of the elements in terms of the generators s 1 and s 2 .