Formula to make combination of elements given possible variables?

gifariz

Board Regular
Joined
May 2, 2021
Messages
128
Office Version
  1. 365
Platform
  1. Windows
I need a formula to make 2D array of combination of a number of elements given an array of possible variables. Below is the sample input output:
ne = 2
x = {a;b;c}
out = {a,a; a,b; a,c; b,a; b,b; b,c; c,a; c,b;, c,c}

ne is number of elements as integer, x is list of variables as 1d array of any length. I would like to have formula without MAKEARRAY, LAMBDA or other slow functions. This is supposed to be an easy combinatoric problem, but I can't find elegant solution. Below is my attempt:
=LET(ne,A1,x,B1:B3,nx,COUNTA(x),i,SEQUENCE(nx^ne,ne,0),r,INT(i/ne)+1,c,MOD(i,ne)+1,comb,TEXT(BASE(SEQUENCE(nx^ne,,0),nx),REPT("0",ne)),INDEX(x,VALUE(MID(INDEX(comb,r),c,1))+1))
It works but too complicated.

Could anyone help? Or maybe someone has made the lambda function in lambda sub-forum? Thanks in advance.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hello, would this work for you:

Excel Formula:
=LET(
ne,A1,
x,B1:B3,
nx,COUNTA(x),
seq,SEQUENCE(nx),
all,MOD(TRUNC(SEQUENCE(nx^ne,,0)/nx^(ne-SEQUENCE(,ne))),nx)+1,
XLOOKUP(all,seq,x))
 
Upvote 1
Solution
Hello, would this work for you:

Excel Formula:
=LET(
ne,A1,
x,B1:B3,
nx,COUNTA(x),
seq,SEQUENCE(nx),
all,MOD(TRUNC(SEQUENCE(nx^ne,,0)/nx^(ne-SEQUENCE(,ne))),nx)+1,
XLOOKUP(all,seq,x))
This is it! Thank you very much!
I recall using this equation many times but it's hard for me to intuit it from scratch.

One thing, looks like using INDEX is more efficient, no lookup
=LET(ne,A1,x,B1:B3,nx,COUNTA(x),INDEX(x,MOD(TRUNC(SEQUENCE(nx^ne,,0)/nx^(ne-SEQUENCE(,ne))),nx)+1))
 
Upvote 0
Just for fun, another option:

Excel Formula:
=LET(ne,A1, x, B1:B4, lx, COUNTA(x), INDEX(x, MID(BASE(SEQUENCE(lx^ne,,0), lx, ne), SEQUENCE(,ne), 1)+1))
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,157
Members
452,615
Latest member
bogeys2birdies

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