Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 7 down vote accepted

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.

share|improve this answer
    
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.

share|improve this answer

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.

share|improve this answer
    
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
1  
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}]
share|improve this answer
1  
FYI: You should not use N like this as it is a reserved Symbol. – Mr.Wizard Feb 17 at 0:11
2  
By the way: +1 for bothering to self answer, and welcome to the site. – Mr.Wizard Feb 17 at 0:15
1  
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

enter image description here

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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