How to Store a Banded Matrix by Diagonal I'm taking a graduate level independent study course this semester in Matrix Computations. I'm not getting much support from the professor, so am turning to the excellent StackExchange community for help (how's that for buttering you up).
The section I'm on right now shows how a symmetric matrix can be stored-by-diagonal (as the book puts it) by storing the main diagonal and each sub diagonal in a one dimension array then doing some creative indexing to get the values out of the array. So,
$$A = \begin{pmatrix} 1 & 2 & 3 \\ 2 & 4 & 5 \\ 3 & 5 & 6\end{pmatrix}$$
would get stored like $A.\mathrm{diag} = [ 1 4 6 2 5 3 ]$. Then if $i \ge j$ the elements of $A$ would be
$$a_{i+k,i} = A.\mathrm{diag}\left(i + nk - \frac{k(k-1)}{2}\right)$$
The problem I am stuck on asks me to come up with a scheme to store a banded matrix in a similar fashion. I can see how to store it by storing the main diagonal, then the first subdiagonal, then the next subdiagonal, ... , then the first superdiagonal, etc. What I can't seem to figure out is how to come up with an indexing scheme that will get the $a_{ij}$ element out of the array.
In other words
$$B = \begin{pmatrix} 1 & 2 & 3 & 0 & 0 \\
                          4 & 5 & 6 & 7 & 0 \\
                          0 & 8 & 9 & 1 & 2 \\
                          0 & 0 & 3 & 4 & 5 \\
                          0 & 0 & 0 & 6 & 7
\end{pmatrix}
$$
would get stored in an array like
$$ B.\mathrm{diag} =\begin{bmatrix}1 & 5 & 9 & 4 & 7 & 4 & 8 & 3 & 6 & 2 & 6 & 1 & 5 & 3 & 7 & 2\end{bmatrix}$$
My question then is what is the right hand side of
$$ b_{ij} = B.\mathrm{diag}(???) $$
I'm less interested in the actual answer and more interested in how to figure out the index. Thanks in advance for any help you can provide.

Wikipedia reveals:

can be stored as:

