# Formulate n different vector dot products as a matrix multiplication

Is there a smart way to formulate the following as one matrix multiplication, $$\sum_{i=1}^nh_i \sum_{k=1}^KH_{ik} p_{ik}$$ as something like $\mathbf{h*(H*p)}$?

The only way I can think of is to augment $\mathbf{H}$ and $\mathbf{p}$ with 0 to be n-by-nK, nK-by-nK matrices respectively.

• This appears to be mathematics rather than stats, and likely belongs on math.SE. – Glen_b Sep 16 '15 at 12:46
• @Glen_b, I see. I thought about where I should put this too. Thanks. Regardless, it got some good answers. I will post accordingly next time! – villybyun Sep 16 '15 at 17:20

Also if you define $*$ as coordinate-wise multiplication, and $\mathbf{1_k}$ as the $k$ dimension column vector, $$\mathbf{ h^T (H * p) 1_k }$$ does the trick. And it should be fewer operations.
If $\mathbf{H}$ and $\mathbf{p}$ are $n\times K$ matrices and $\mathbf{h}$ is $n\times 1$ vector then you write this product as
$$\mathbf{h}^T\mathrm{diag}(\mathbf{H}\mathbf{p}^T)$$
where $\mathrm{diag}$ is the operator which takes a matrix and returns its main diagonal as a vector.