In MATLAB, matrix computation is very efficient. Thus, we should change the computation into matrix forms as far as possible.
I have the following formula: $$ \frac{\partial L}{\partial z_j} = \sum_{i=1}^n (x_i-z_j) Q_{ij} P_{ij}=\sum_{i=1}^n x_i Q_{ij} P_{ij} -z_j \sum_{i=1}^n Q_{ij} P_{ij} $$ where $L \in R$ is a scalar function about $X\in R^{d\times n}$ and $Z\in R^{d\times m}$, and $P,Q\in R^{n\times m}$, and $x_i$ is the $i$-th column of $X$, $z_j$ is the $j$-th column of $Z$.
How could I change the above equation into the following matrix form? $$ \frac{\partial L}{\partial Z} = X(Q\circ P) - Z\ \mbox{diag}({(Q\circ P)}^T 1_n) $$ where $\circ$ is the Hadamard(element-wise) product, and $\mbox{diag}(a)$ is make the vector $a\in R^{m\times 1}$ into a diagonal matrix with the diagonal elements be the elements of $a$, and $1_n={[1,1,\cdots,1]}^T \in R^{n\times 1}$ is a vector of all ones.
Could anyone help give some advice?
Rather than working with column vectors, let's add an extra index and work with the full matrix components:
$$ \eqalign { \frac{\partial L}{\partial Z_{kj}} &= \sum_{i=1}^n X_{ki}Q_{ij}P_{ij} - Z_{kj}\sum_{i=1}^n Q_{ij}P_{ij} \cr } $$ Substituting $H = Q\circ P$ simplifies the equation to $$ \eqalign { \frac{\partial L}{\partial Z_{kj}} &= \sum_{i=1}^n X_{ki}H_{ij} - Z_{kj}\sum_{i=1}^n H_{ij} \cr } $$ Next, introduce the 3rd order tensor $\beta$ whose components $\beta_{ijk} = 1$ when $i\!=\!j\!=\!k$ but are zero otherwise. This tensor allow you to write diagonal operations in an algebraically convenient form: $$ \eqalign { \beta\cdot\omega &= {\rm Diag}(\omega) = \Omega \cr \beta:\Omega &= {\rm diag}(\Omega) = \omega \cr } $$ Now we can eliminate the explicit summations and employ the Einstein summation convention: $$ \eqalign { \frac{\partial L}{\partial Z_{kj}} &= X_{ki}H_{ij} - Z_{kp}\beta_{pjl}H_{il} 1_{i} \cr &= X_{ki}H_{ij} - Z_{kp}\beta_{pjl}H^T_{li} 1_{i} \cr } $$ Finally, we can progress from the summation convention to a matrix equation, and simplify: $$ \eqalign { \frac{\partial L}{\partial Z} &= X\cdot H - Z\cdot\beta\cdot(H^T\cdot 1) \cr &= X\cdot H - Z\cdot{\rm Diag}(H^T\cdot 1) \cr } $$ Update:
Sometimes (e.g. working with the Hadamard product of 2 vectors) you must suspend the Einstein convention and write something like $$ \eqalign { w_j &= u_j v_j \cr } $$ Another useful property of $\beta$ (which I used above) is to not have to suspend the convention and instead write $$ \eqalign { w_j &= u_i \beta_{ijk} v_k \cr } $$ In other words: $u\circ v = u\cdot\beta\cdot v$