SUBARRAYS

=SUBARRAYS(arr,r,c)

arr
target array to extract subarrays from
r
row dimension of subarrays to extract
c
column dimension of subarrays to extract

Recursively traverse an array from top-to-bottom, left-to-right, extracting subarrays with provided dimensions

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
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

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
ABCDEFG
13 x 42 x 1
2ABCD{"A";"E"}
3EFGH{"B";"F"}
4IJKL{"C";"G"}
5{"D";"H"}
6{"E";"I"}
7{"F";"J"}
8{"G";"K"}
9{"H";"L"}
10
113 x 3
12{"A","B","C";"E","F","G";"I","J","K"}
13{"B","C","D";"F","G","H";"J","K","L"}
14
153 x 5
16User dimension exceeds array dimension
17
Subarrays
Cell Formulas
RangeFormula
B2:E4B2=CHAR(SEQUENCE(3,4,65,1))
G2:G9G2=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:G13G12=SubArrays(B2#,3,3)
G16G16=SubArrays(B2#,3,5)
Dynamic array formulas.
 
Last edited by a moderator:
Upvote 0
Recursion limits were making this impractical for larger dimension arrays to extract smaller dimension subarrays, so I derived a non-recursive analogue. There is a trade-off though as you can no longer perform operations on the individual subarrays in the LAMBDA, have to pick your methodology based on use case.

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,
                irCt,subArrCt,
                icCt,r*c,
                rIndex,1+INT(SEQUENCE(irCt,icCt,0,1)/(cCt*icCt))+TRANSPOSE(INT(SEQUENCE(icCt,irCt,1/((irCt*c)+1),1/((irCt*c))))),
                cIndex,1+MOD(SEQUENCE(irCt,icCt,0,1),c)+MOD(SEQUENCE(irCt,1,0,1),cCt),
                INDEX(arr,rIndex,cIndex)
            )
        )
    )
)
 

Forum statistics

Threads
1,224,816
Messages
6,181,139
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