matrix with all possible combinations of 8!

1 view (last 30 days)
Erika Puiutta
Erika Puiutta on 30 Jan 2019
Answered: Erika Puiutta on 30 Jan 2019
Hello!
I have a vector a = 1:8 and want all different combinations of these numbers where each number can only occur once in each row. So i want
12345678
12345687
12345867
12345876
and so on.
I've been stuck on this for hours, I have googled and used allcomb but that leads to rows like 11111118 and I can't seem to figure out how to only get the rows where each number is represented only once. Can anyone help me?

Answers (3)

madhan ravi
madhan ravi on 30 Jan 2019

Jos (10584)
Jos (10584) on 30 Jan 2019
You need permutations not combinations (which my function allcomb returns).
Take a look at PERMS
>> perms(1:3)
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3

Erika Puiutta
Erika Puiutta on 30 Jan 2019
Thank you guys so much!
I was so sure there would be a single function that does this, but I just couldn't find it for the life of me. Thanks again!

Community Treasure Hunt

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

Start Hunting!