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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

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