I'm having issues figuring out how find the derivative of L wrt $c_{ni}$ in this case. $a_n$ and $c_{ni}$ are both vectors. Here's what I have so far:
$$L = \sum_{n=0}^N\| a_n- \sum_{i=0}^Bc_{ni}d_i\|^2$$ $$L' = \sum_{n=0}^N\|2* \sum_{i=0}^Bc_{ni}d_i * d_i\|$$ $$L' = \sum_{n=0}^N\|2* \sum_{i=0}^Bc_{ni} * \sum_{i=0}^Bd_i^2\| $$ $$L' = \sum_{n=0}^N\|2* B * c_{ni} * B * d_i^2\| $$ $$L' = N * \|2* B * c_{ni} * B * d_i^2\| $$
But this doesn't seem right... I'm not sure what to do with the $d_i$ in the first step of the derivative.
Let $C_n$ be the matrix whose $i$th column is $c_{ni}$, and let $$ d = \begin{bmatrix} d_1 \\ \vdots \\ d_B \end{bmatrix}. $$ Note that $$L(d) = \sum_{n=0}^N \| C_n d - a_n \|^2.$$ From the multivariable chain rule, the gradient of $L$ is $$ \nabla L(d) = \sum_{n=0}^N 2 C_n^T(C_n d - a_n). $$ (Here we are using the convention that the gradient is a column vector rather than a row vector.)