# How do I build a patterned matrix (high-pass and/or low-pass filter bank)?

I would like the shortest code to build the following matrices, of arbitrary dimension $N$:

A Low-Pass filter bank matrix is of the form

$\begin{bmatrix} 1 & 1 & 0 & 0 & \dots & 0 & 0 & 0 \\ & 1 & 1 & 0 & \dots & 0 & 0 & 0 \\ & &1 & 1 & \dots & 0 & 0 & 0 \\ & & & & \ddots & \vdots & \vdots & \vdots \\ & & & & \dots & 1 & 1 & 0 \\ & & & & \dots & & 1 & 1 \\ & & & & \dots & & & 1 \end{bmatrix} _{N\times N}$

A High-Pass filter bank matrix is of the form:

$\begin{bmatrix} 1 & -1 & 0 & 0 & \dots & 0 & 0 & 0 \\ & 1 & -1 & 0 & \dots & 0 & 0 & 0 \\ & &1 & -1 & \dots & 0 & 0 & 0 \\ & & & & \ddots & \vdots & \vdots & \vdots \\ & & & & \dots & 1 & -1 & 0 \\ & & & & \dots & & 1 & -1 \\ & & & & \dots & & & 1 \end{bmatrix} _{N\times N}$

How can I build the above matrices for any dimension $N$?

-

Taking your self-answer as a guide you can still use SparseArray and Band

n = 5;

SparseArray[{Band[{1, 1}] -> 1, Band[{1, 2}] -> 1}, {n, n}] // Grid


$\begin{matrix} 1 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 \\ 0 & 0 & 1 & 1 & 0 \\ 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 1 \\ \end{matrix}$

SparseArray[{Band[{1, 1}] -> 1, Band[{1, 2}] -> -1}, {n, n}] // Grid


$\begin{matrix} 1 & -1 & 0 & 0 & 0 \\ 0 & 1 & -1 & 0 & 0 \\ 0 & 0 & 1 & -1 & 0 \\ 0 & 0 & 0 & 1 & -1 \\ 0 & 0 & 0 & 0 & 1 \\ \end{matrix}$

Use Normal to convert to a standard list-of-lists if you need it.

-
Unfortunately, the lower triangle needs to be all ZEROS. Thanks though :) – Lewkrr Feb 17 at 0:08

One way is to use DiagonalMatrix

n = 5;
DiagonalMatrix[ConstantArray[1, n]] + DiagonalMatrix[ConstantArray[-1, n - 1], 1]


for the highpass and

DiagonalMatrix[ConstantArray[1, n]] + DiagonalMatrix[ConstantArray[1, n - 1], 1]


for the lowpass.

-

Also for fun:

lowpass[n_Integer] := ToeplitzMatrix[UnitVector[n, 1], PadRight[{1, 1}, n]]

highpass[n_Integer] := ToeplitzMatrix[UnitVector[n, 1], PadRight[{1, -1}, n]]


In practice, I'd use Wizard's route, however.

-
I don't understand the purpose of the LaTeX edit you made to my answer. I just copied what Mathematica gave me; is it inferior somehow? – Mr.Wizard Feb 17 at 16:57
It's just that the matrix environment tends to be less finicky than the more general array. OTOH, I suppose Mathematica is not aware of the AMS extension to $\LaTeX$, which is why it sticks with array and compensates with all those alignment instructions (those multiple c's you may have noticed). – J. M. Feb 17 at 17:01

I figured it out. The result used Table[] and If[]

To get the low pass, use:

Table[If[i == j, 1, 0] + If[i == j - 1, 1, 0], {i, N}, {j, N}]


To get the high pass, use:

Table[If[i == j, 1, 0] + If[i == j - 1, -1, 0], {i, N}, {j, N}]

-
FYI: You should not use N like this as it is a reserved Symbol. – Mr.Wizard Feb 17 at 0:11
By the way: +1 for bothering to self answer, and welcome to the site. – Mr.Wizard Feb 17 at 0:15
Alternatively, Table[KroneckerDelta[i, j] + KroneckerDelta[i, j - 1], {i, n}, {j, n}] and Table[KroneckerDelta[i, j] - KroneckerDelta[i, j - 1], {i, n}, {j, n}] – march Feb 17 at 0:29
@Mr.Wizard Thanks for the help! And I will not use N like that :) – Lewkrr Feb 18 at 19:23

For fun:

With[{n = 5}, ArrayPad[{1, 1}, {# - 1, n - # - 1}] & /@ Range[n]] // Grid
With[{n = 5}, ArrayPad[{1, -1}, {# - 1, n - # - 1}] & /@ Range[n]] // Grid


-
n = 5;
ReplacePart[IdentityMatrix[n], {i_, j_ } /; j == i + 1 -> 1] // Grid
ReplacePart[IdentityMatrix[n], {i_, j_ } /; j == i + 1 -> -1] // Grid

-