Linear algebra formulation of an averaging Suppose we have the following symmetric matrices $d$ and $m,$ whose elements are real and nonnegative, with their diagonals all zeroes:
$$d=\begin{pmatrix}
  0 & d_{12} & d_{13}\\ 
  d_{21} & 0 & d_{23}\\
  d_{31} & d_{32} & 0
\end{pmatrix},$$
$$m=\begin{pmatrix}
  0 & m_{12} & m_{13}\\ 
  m_{21} & 0 & m_{23}\\
  m_{31} & m_{32} & 0
\end{pmatrix}.$$
I am trying to perform an averaging over different subsets of elements of $m$ based on values in $d:$ with the set of unique values in $d$ given by $u=\mathop{Set}\{d_{12},d_{13},d_{23}\}$ [*], there is an average $\langle m_i\rangle$ for $i\in u,$ such that $$\langle m_i\rangle=\frac{1}{n_i} \sum_{\{a,b\}|d_{a,b}=i}m_{a,b} $$
where for a given $i\in u$ the sum goes over all matrix indices $\{a,b\}$ where $d_{a,b}=i,$ and $n_i$ is the count of times value $i$ occurs in $d.$ 
[*]: for general square symmetric matrices $d$, $u$ is the mathematical set of entries in the upper or lower triangular part of $d.$
Below is an example:

$$d=\begin{pmatrix}
  0 & 1 & 5\\ 
  1 & 0 & 5\\
  5 & 5 & 0
\end{pmatrix},$$
then $u=\{1,5\},$ $n_1=2,$ and $n_5=4$ and the corresponding means of $m$ are:


*

*$\langle m_1 \rangle= \frac{1}{2} (m_{12}+m_{21}),$

*$\langle m_5 \rangle= \frac{1}{4} (m_{13}+m_{23}+m_{31}+m_{32}).$ 

Question:


*

*Is there a linear algebraic way of expressing the $\langle m_i\rangle$'s in terms of $d$ and $m?$ For instance, by considering ways of partitioning the entries of $m$ matrix based on $d$ and taking the matrix product with an appropriately defined matrix to obtain $\langle m_i\rangle $ as columns or rows of the resulting matrix. 


To clarify better what I mean by linear algebraic formulation, here's an example: To compute the sum of all entries in a matrix $A,$ we can use the product: $\mathbf 1^\top\mathbf A\mathbf 1$, where $\mathbf 1$ is the column vector of all ones, which makes the task computationally very efficient.
 A: The most direct way to do this using linear algebra is to use the Euclidean norm of matrices: $\langle A, B\rangle = \text{Tr}(A^TB)$.  The specific way that I would construct this would be the following: Once you identify the distinct parts of $d$ that you want to isolate, split the matrix up into its distinct parts.  Using your example:
$$
d \;\; =\;\; \begin{pmatrix}
  0 & 1 & 5\\ 
  1 & 0 & 5\\
  5 & 5 & 0
\end{pmatrix} \;\; =\;\; \underbrace{\begin{pmatrix}
  0 & 1 & 0\\ 
  1 & 0 & 0\\
  0 & 0 & 0
\end{pmatrix}}_{ = E_1} + \underbrace{\begin{pmatrix}
  0 & 0 & 5\\ 
  0 & 0 & 5\\
  5 & 5 & 0
\end{pmatrix}}_{=E_5}.
$$
Then if we compute $\langle \frac{1}{5}E_5, m\rangle$ we will get:
\begin{eqnarray*}
\left \langle \frac{1}{5}E_5, m \right \rangle & = & \text{Tr} \left [\begin{pmatrix}
  0 & 0 & 1\\ 
  0 & 0 & 1\\
  1 & 1 & 0
\end{pmatrix}\begin{pmatrix}
  0 & m_{12} & m_{13}\\ 
  m_{21} & 0 & m_{23}\\
  m_{31} & m_{32} & 0
\end{pmatrix}  \right ] \\
& = & \text{Tr} \left [\begin{pmatrix}
m_{31} & m_{32} & 0 \\
m_{31} & m_{32} & 0 \\
m_{21} & m_{12} & m_{13} + m_{23} \\
\end{pmatrix} \right ] \\
& = & m_{13} + m_{31} + m_{23} + m_{32}.
\end{eqnarray*}
The most general way to accomplish this then is to compute
$$
\langle m_k\rangle \;\; =\;\; \frac{1}{k\cdot n_k} \text{Tr}\left (E_km\right ).
$$
