# In Matlab: how do I quickly create smoothing matrices?

Say I have 5 measurements of a signal as a column vector: x. I can smooth the signal by multiplying it with a smoothing matrix:

$\ \mathbf{xs=S*x}$

For instance I might use [1 2 1] as a smoothing kernel in which case I get:

S =
2     1     0     0     0
1     2     1     0     0
0     1     2     1     0
0     0     1     2     1
0     0     0     1     2


So far I have been using the function sparse() to build the matrix diagonal by diagonal, but this quickly becomes tedious.

How can I quickly create a mxm smoothing matrix from a 1xn convolution kernel in Matlab?

• Why don't you use filter instead? Something like filter([1 2 1],1,x) Jul 5 '12 at 16:24
• Convolve seems a good candidate, too... Jul 5 '12 at 18:24

You could do the following-

kernel = [1 2 1];
s = conv2(eye(numMeasurements), kernel, 'same')

• Thanks Jim. That works fine, but is there not some matrix operation: S=operation(eye(n,n),kernel) ?
– Andy
Jul 5 '12 at 16:46
• Not that I'm aware of. Jul 5 '12 at 16:50
• @Andy Changed the solution to make it much more compact and more like you want, I think. Jul 5 '12 at 20:23
• Thanks Jim! Something like that was exactly what I was looking for.
– Andy
Jul 6 '12 at 10:53
• Sorry I can't comment yet, but just in case you didn't know, you could define a function in another .m file and use it like your suggested "S=operation(eye(n,n),kernel)" if that's what you really want. Jul 6 '12 at 13:27