6
$\begingroup$

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$?

$\endgroup$

6 Answers 6

7
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Unfortunately, the lower triangle needs to be all ZEROS. Thanks though :) $\endgroup$
    – Lewkrr
    Feb 17, 2016 at 0:08
5
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Feb 17, 2016 at 16:57
  • 1
    $\begingroup$ 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). $\endgroup$ Feb 17, 2016 at 17:01
3
$\begingroup$

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}]
$\endgroup$
4
  • 1
    $\begingroup$ FYI: You should not use N like this as it is a reserved Symbol. $\endgroup$
    – Mr.Wizard
    Feb 17, 2016 at 0:11
  • 2
    $\begingroup$ By the way: +1 for bothering to self answer, and welcome to the site. $\endgroup$
    – Mr.Wizard
    Feb 17, 2016 at 0:15
  • 1
    $\begingroup$ 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}] $\endgroup$
    – march
    Feb 17, 2016 at 0:29
  • $\begingroup$ @Mr.Wizard Thanks for the help! And I will not use N like that :) $\endgroup$
    – Lewkrr
    Feb 18, 2016 at 19:23
3
$\begingroup$

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

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.