CONVOLUTION

CONVOLUTION(set_1,set_2)
set_1
vertical range of numbers
set_2
vertical range of numbers

Return the convolution of 2 sets of numbers

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,963
Office Version
  1. 365
Platform
  1. Windows
CONVOLUTION returns the convolution of 2 sets of numbers. I was watching a 3Blue1Brown video where they explain the process and I thought it would be fun to come up with a lambda for it. The 2 sets of numbers should be vertical ranges with the same amount of cells in each range. In the example below, there is matrix multiplication being done to the 2 sets, and the result is the addition of the diagonals, which are color coded.

CONVOLUTION
ABCDEFGHIJKL
1456ElementMathResultSet1Set2Convolution
2145611*44144
328101221*5+2*4132513
4312151831*6+2*5+3*4283628
542*6+3*52727
653*61818
Sheet6
Cell Formulas
RangeFormula
L2:L6L2=CONVOLUTION(J2:J4,K2:K4)
Dynamic array formulas.


Excel Formula:
=LAMBDA(set_1,set_2,
    LET(
        a,set_1,
        b,set_2,
        c,COUNTA(a),
        MAP(
            SEQUENCE(c*2-1,,-c+1),
            LAMBDA(x,
                LET(
                    sx,DROP(SEQUENCE(,c),,x),
                    cx,COUNTA(sx),
                    rx,INDEX(sx,SEQUENCE(,cx,cx,-1)),
                        SUM(INDEX(a,sx)*INDEX(b,rx))
                )
            )
        )
    )
)
 
Last edited:
Upvote 0
Last one reads a bit easier, but it can be reduced to.

Excel Formula:
=LAMBDA(
    set_1,set_2,
    LET(
        c,COUNTA(set_1),
        MAP(
            SEQUENCE(c*2-1,,-c+1),
            LAMBDA(
                x,
                LET(
                    sx,DROP(SEQUENCE(,c),,x),
                    cx,COUNTA(sx),
                        SUM(INDEX(set_1,sx)*INDEX(set_2,INDEX(sx,SEQUENCE(,cx,cx,-1))))
                )
            )
        )
    )
)
 
I noticed that the above formulas would fail if the 2 datasets weren't the same number of cells.

The below revision fixes this issue. And, is a complete departure from my original strategy.

CONVOLUTION
JKLMNO
19123Result
2011231
2122464
22336910
234481216
2417
2512
Sheet5
Cell Formulas
RangeFormula
K20:M23K20=MMULT(J20:J23,K19:M19)
O20:O25O20=CONVOLUTION(J20:J23,K19:M19)
Dynamic array formulas.


Excel Formula:
=LAMBDA(
    set_1,set_2,
    LET(
        a,set_1,
        b,set_2,
        ca,COUNTA(a),
        cb,COUNTA(b),
        m,MMULT(set_1,set_2),
        s,SEQUENCE(ca+cb-1),
        g,MAKEARRAY(
            ca,cb,
            LAMBDA(
                r,c,
                IF(OR(AND(r=1,c>1),AND(c=1,r>1)),
                    r*c,
                    r*c-((r-1)*(c-1))
                )
            )),
            MAP(
                s,
                LAMBDA(
                    x,
                        SUMPRODUCT((g=x)*m)
                )
            )
        )
)
 

Forum statistics

Threads
1,224,836
Messages
6,181,252
Members
453,028
Latest member
letswriteafairytale

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