Permutations Dynamic Array Formulas

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,957
Office Version
  1. 365
Platform
  1. Windows
Using the dynamic array functions, I created a formula that will return permutations, with or without repetitions, based on a range of values you select. I used the lambda function to turn it into an easy-to-use formula.

Just wanted to share because I think it's cool, permutations come up a lot, and I'd like to see what everyone thinks. I'm sure that this formula could be optimized or shortened.

permutationx.xlsm
ABCDEFGH
1ValuesRepitionsResults
2Dog0DogCatBirdLizard
3CatDogCatLizardBird
4BirdDogBirdCatLizard
5LizardDogBirdLizardCat
6DogLizardCatBird
7DogLizardBirdCat
8CatDogBirdLizard
9CatDogLizardBird
10CatBirdDogLizard
11CatBirdLizardDog
12CatLizardDogBird
13CatLizardBirdDog
14BirdDogCatLizard
15BirdDogLizardCat
16BirdCatDogLizard
17BirdCatLizardDog
18BirdLizardDogCat
19BirdLizardCatDog
20LizardDogCatBird
21LizardDogBirdCat
22LizardCatDogBird
23LizardCatBirdDog
24LizardBirdDogCat
25LizardBirdCatDog
Sheet3
Cell Formulas
RangeFormula
E2:H25E2=PermutX(A2:A5,C2)
Dynamic array formulas.


permutationx.xlsm
ABCDEFG
1ValuesRepitionsResults
2Dog1DogDogDog
3CatDogDogCat
4BirdDogDogBird
5DogCatDog
6DogCatCat
7DogCatBird
8DogBirdDog
9DogBirdCat
10DogBirdBird
11CatDogDog
12CatDogCat
13CatDogBird
14CatCatDog
15CatCatCat
16CatCatBird
17CatBirdDog
18CatBirdCat
19CatBirdBird
20BirdDogDog
21BirdDogCat
22BirdDogBird
23BirdCatDog
24BirdCatCat
25BirdCatBird
26BirdBirdDog
27BirdBirdCat
28BirdBirdBird
Sheet3
Cell Formulas
RangeFormula
E2:G28E2=PermutX(A2:A5,C2)
Dynamic array formulas.


Name Manager Lambda function "PermutX"
Excel Formula:
=LAMBDA(
    range,
    repetition,
        LET(v,range,
        c,COUNTA(v),
        n,MID(TEXT(BASE(SEQUENCE(PERMUTATIONA(c,c),,0),c),REPT("0",c)),SEQUENCE(,c),1)+1,
        pa,INDEX(v,n),nj,BYROW(pa,LAMBDA(ro,TEXTJOIN(,,ro))),
        f,BYROW(nj,LAMBDA(ro,SUM(IF(LEN(ro)-LEN(SUBSTITUTE(ro,v,"")),1,0))=c)),
        IF(repetition,pa,FILTER(pa,f))
        )
    )
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Another option for that
Fluff.xlsm
ABCDEFGHIJK
1ValuesRepitionsResults
2Dog0DogCatBird1DogDogDog
3CatDogBirdCatDogDogCat
4BirdCatDogBirdDogDogBird
5CatBirdDogDogCatDog
6BirdDogCatDogCatCat
7BirdCatDogDogCatBird
8DogBirdDog
9DogBirdCat
10DogBirdBird
11CatDogDog
12CatDogCat
13CatDogBird
14CatCatDog
15CatCatCat
16CatCatBird
17CatBirdDog
18CatBirdCat
19CatBirdBird
20BirdDogDog
21BirdDogCat
22BirdDogBird
23BirdCatDog
24BirdCatCat
25BirdCatBird
26BirdBirdDog
27BirdBirdCat
28BirdBirdBird
Data
Cell Formulas
RangeFormula
E2:G7E2=LET(a,A2:A4,r,ROWS(a),b,MID(BASE(SEQUENCE(r^r,,0),r,r),SEQUENCE(,r),1)+1,c,BYROW(b,LAMBDA(br,SUM(--(MMULT(--(br=TRANSPOSE(br)),SEQUENCE(r,,,0))=1))=r)),d,INDEX(a,b),IF(C2,d,FILTER(d,c)))
I2:K28I2=LET(a,A2:A4,r,ROWS(a),b,MID(BASE(SEQUENCE(r^r,,0),r,r),SEQUENCE(,r),1)+1,c,BYROW(b,LAMBDA(br,SUM(--(MMULT(--(br=TRANSPOSE(br)),SEQUENCE(r,,,0))=1))=r)),d,INDEX(a,b),IF(H2,d,FILTER(d,c)))
Dynamic array formulas.
 
Upvote 0
I like this @Fluff. One blind spot I've always had with Excel formulas is the MMULT function. Stepping through I see what is going on and think I have a better understanding of how to use it. Might be a while before I integrate it in a formula organically, but thanks.
 
Upvote 0
I never used to understand it, but saw a couple of posts (I think by Eric W) where it was explained & I've found it very useful.
 
Upvote 0
Realised there is no need for the MMULT, so a shorter version
Excel Formula:
=LET(a,A2:A5,r,ROWS(a),b,MID(BASE(SEQUENCE(r^r,,0),r,r),SEQUENCE(,r),1)+1,c,BYROW(b,LAMBDA(br,SUMPRODUCT(--(br=TRANSPOSE(br)))=r)),d,INDEX(a,b),IF(I2,d,FILTER(d,c)))
 
Upvote 0
Solution
Realised there is no need for the MMULT, so a shorter version
Excel Formula:
=LET(a,A2:A5,r,ROWS(a),b,MID(BASE(SEQUENCE(r^r,,0),r,r),SEQUENCE(,r),1)+1,c,BYROW(b,LAMBDA(br,SUMPRODUCT(--(br=TRANSPOSE(br)))=r)),d,INDEX(a,b),IF(I2,d,FILTER(d,c)))
Very cool! Same idea as the MMULT, just a more direct way. Nice.
 
Upvote 0
I saw a post on another Excel site once that said "Can you do all kinds of formulas, including SUMPRODUCT and array functions, but just can't figure out MMULT?" I took that as a challenge to figure it out. It's very useful, but sometimes there are better alternatives, like here.

I haven't tried my hand at combination functions and dynamic arrays yet. I finally got LAMBDA, but I'm still missing BYROW, so I'm still behind the curve.
 
Upvote 0
I saw a post on another Excel site once that said "Can you do all kinds of formulas, including SUMPRODUCT and array functions, but just can't figure out MMULT?" I took that as a challenge to figure it out. It's very useful, but sometimes there are better alternatives, like here.

I haven't tried my hand at combination functions and dynamic arrays yet. I finally got LAMBDA, but I'm still missing BYROW, so I'm still behind the curve.
I did something similar today, looking at examples of using MMULT. Finally going to force myself to learn it.

Recently got the update for BYROW, BYCOL, etc. and getting to know those. Don't know if this would be possible without them.
 
Upvote 0
Recently got the update for BYROW, BYCOL, etc. and getting to know those. Don't know if this would be possible without them.

If it is I don't know how, I tried to do it without BYROW & failed.

Well, it's barely possible. I like challenges, so I took a shot at it. Fluff pretty much nailed the permutations part of it, so I just stole copied that part of the formula. Removing the duplicates was tougher, but I managed. Performance wise, this is a terrible function. 3 and 4 work ok, 5 items take 20 seconds, and I haven't tried anything bigger. BYROW is definitely an improvement.

Book1
ABCDEFGHIJKL
1ValuesRepeatRepeat
2Dog0DogCatBird1DogDogDog
3CatDogBirdCatDogDogCat
4BirdCatDogBirdDogDogBird
5CatBirdDogDogCatDog
6BirdDogCatDogCatCat
7BirdCatDogDogCatBird
8DogBirdDog
9DogBirdCat
10DogBirdBird
11CatDogDog
12CatDogCat
13CatDogBird
14CatCatDog
15CatCatCat
16CatCatBird
17CatBirdDog
18CatBirdCat
19CatBirdBird
20BirdDogDog
21BirdDogCat
22BirdDogBird
23BirdCatDog
24BirdCatCat
25BirdCatBird
26BirdBirdDog
27BirdBirdCat
28BirdBirdBird
Sheet16
Cell Formulas
RangeFormula
D2:F7,J2:L28D2=LET(aa,$A2:$A10,a,FILTER(aa,aa<>""),r,ROWS(a),b,MID(BASE(SEQUENCE(r^r,,0),r,r),SEQUENCE(,r),1)+1,sq,SEQUENCE(r),s,MOD(SMALL(b+SEQUENCE(r^r,,10,10),SEQUENCE(r^r,r)),10),m,MMULT(s,10^sq),ix,INDEX(a,b),IF(C2=1,ix,FILTER(ix,m=SUM(sq*(10^sq)))))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top