This LAMBDA provides a recursive loop to extract subarrays of given set of dimensions, or warns the user if their provided dimension(s) are bigger than their target array; I am returning the list of the subarrays using ARRAYTOTEXT, but this could be substituted with another function to suit the purpose of the user (i.e. if getting sums of each subarray, etc.)
Uses List.Combine
Uses List.Combine
Excel Formula:
=LAMBDA(arr,r,c,
LET(
m,ROWS(arr),
n,COLUMNS(arr),
IF(OR(r>m,c>n),"User dimension exceeds array dimension",
LET(
rCt,(m-r+1),
cCt,(n-c+1),
subArrCt,rCt*cCt,
LoopTraverseArr,
LAMBDA(self,i,stopCt,
LET(
current,
INDEX(
arr,
SEQUENCE(r,1,1+QUOTIENT(i-1,cCt),1),
SEQUENCE(1,c,1+MOD(i-1,cCt),1)
),
value,ARRAYTOTEXT(current,1),
return,
IF(
i<stopCt,self(self,i+1,stopCt),
value
),
IF(
i=stopCt,return,
List.Combine(value,return)
)
)
),
LoopTraverseArr(LoopTraverseArr,1,subArrCt)
)
)
)
)
LAMBDA_Subarrays.xlsx | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | 3 x 4 | 2 x 1 | |||||||
2 | A | B | C | D | {"A";"E"} | ||||
3 | E | F | G | H | {"B";"F"} | ||||
4 | I | J | K | L | {"C";"G"} | ||||
5 | {"D";"H"} | ||||||||
6 | {"E";"I"} | ||||||||
7 | {"F";"J"} | ||||||||
8 | {"G";"K"} | ||||||||
9 | {"H";"L"} | ||||||||
10 | |||||||||
11 | 3 x 3 | ||||||||
12 | {"A","B","C";"E","F","G";"I","J","K"} | ||||||||
13 | {"B","C","D";"F","G","H";"J","K","L"} | ||||||||
14 | |||||||||
15 | 3 x 5 | ||||||||
16 | User dimension exceeds array dimension | ||||||||
17 | |||||||||
Subarrays |
Cell Formulas | ||
---|---|---|
Range | Formula | |
B2:E4 | B2 | =CHAR(SEQUENCE(3,4,65,1)) |
G2:G9 | G2 | =LAMBDA(arr,r,c, LET( m,ROWS(arr), n,COLUMNS(arr), IF(OR(r>m,c>n),"User dimension exceeds array dimension", LET( rCt,(m-r+1), cCt,(n-c+1), subArrCt,rCt*cCt, LoopTraverseArr, LAMBDA(self,i,stopCt, LET( current,INDEX(arr,SEQUENCE(r,1,1+QUOTIENT(i-1,cCt),1),SEQUENCE(1,c,1+MOD(i-1,cCt),1)), value,ARRAYTOTEXT(current,1), return, IF( i<stopCt,self(self,i+1,stopCt), value ), IF( i=stopCt,return, List.Combine(value,return) ) ) ), LoopTraverseArr(LoopTraverseArr,1,subArrCt) ) ) ) )(B2#,2,1) |
G12:G13 | G12 | =SubArrays(B2#,3,3) |
G16 | G16 | =SubArrays(B2#,3,5) |
Dynamic array formulas. |
Last edited by a moderator:
Upvote
0