A complex one-line way to create such a convolution matrix:
M = sign(conv2(eye(matrix_size),ones(band_width+1),'same'));
the identification matrix is created from a given size, then folded into 2-D with a square matrix, then converted to zeros and ones, taking sign .
The above is great for creating relatively small, non-sparse matrices. For large matrices, convolution can become expensive, and you probably want to present the result as SPDIAGS instead :
M = spdiags(ones(matrix_size,2*band_width+1),... -band_width:band_width,matrix_size,matrix_size);
source share