5

I am trying to create something similar to the below image. I have something, but my formatting is a little off. How can I correct the formatting and also add the W1, W2, Wn and D letters on top (not a major issue however)?

Enter image description here

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{align}
   \begin{bmatrix}
           W_{11} \\
           W_{12} \\
           \vdots \\
           W_{1n}
   \end{bmatrix}
   &+ \begin{bmatrix}
           W_{21} \\
           W_{22} \\
           \vdots \\
           W_{2n}
       \end{bmatrix}
   &+ \hdots
   &+ \begin{bmatrix}
           W_{n1} \\
           W_{n2} \\
           \vdots \\
           W_{nn}
       \end{bmatrix}
   &= \begin{bmatrix}
           \frac{W_{11} + W_{21} + \hdots + W_{n1}}{n} \\
           \frac{W_{12} + W_{22} + \hdots + W_{n2}}{n} \\
           \vdots \\
           \frac{W_{1n} + W_{2n} + \hdots + W_{nn}}{n}
    \end{bmatrix}
\end{align}

\end{document}

4 Answers4

6

The following sets the arrangement inside an array, with the first row handling the labels on top of each bmatrix, while the second row sets each of the matrices/vectors. \arraystretch has been increased to spread the content vertically.

enter image description here

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\[
  \setlength{\arraycolsep}{0pt}% No column separation in array; manual spacing
                               % by supplying empty groups {} around operators
  \renewcommand{\arraystretch}{1.5}% Stretch out content vertically
  \begin{array}{ *{8}{c} }
    % Header row in \scriptstyle
    \scriptstyle W_1 & & % + 
    \scriptstyle W_2 & & % + ... +
    \scriptstyle W_n & & & % = 1/n
    \scriptstyle D \\
    % Matrix/vector row
    \begin{bmatrix}
      W_{11} \\ W_{12} \\ \vdots \\ W_{1n}
    \end{bmatrix}
    & {} + {} &
    \begin{bmatrix}
      W_{21} \\ W_{22} \\ \vdots \\ W_{2n}
    \end{bmatrix}
    & {} + \dots + {} &
    \begin{bmatrix}
      W_{n1} \\ W_{n2} \\ \vdots \\ W_{nn}
    \end{bmatrix}
    & {} = {} &
    \dfrac{1}{n} &
    \begin{bmatrix}
      W_{11} + W_{21} + \dots + W_{n1} \\
      W_{12} + W_{22} + \dots + W_{n2} \\
      \vdots \\
      W_{1n} + W_{2n} + \dots + W_{nn}
    \end{bmatrix}
  \end{array}
\]

\end{document}

Since scalar multiplication of a vector results in each component being multiplied by the scalar, some simplification in the presentation makes things line up nicely.

For numbering the equation, you probably want to consider the following solution; it sets the equation number on the math axis.

enter image description here

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{equation}
  \setlength{\arraycolsep}{0pt}% No column separation in array; manual spacing
                               % by supplying empty groups {} around operators
  \renewcommand{\arraystretch}{1.5}% Stretch out content vertically
  \mathop{\begin{bmatrix}
    W_{11} \\ W_{12} \\ \vdots \\ W_{1n}
  \end{bmatrix}}^{W_1}
  +
  \mathop{\begin{bmatrix}
    W_{21} \\ W_{22} \\ \vdots \\ W_{2n}
  \end{bmatrix}}^{W_2}
  + \dots +
  \mathop{\begin{bmatrix}
    W_{n1} \\ W_{n2} \\ \vdots \\ W_{nn}
  \end{bmatrix}}^{W_n}
  =
  \dfrac{1}{n}
  \mathop{\begin{bmatrix}
    W_{11} + W_{21} + \dots + W_{n1} \\
    W_{12} + W_{22} + \dots + W_{n2} \\
    \vdots \\
    W_{1n} + W_{2n} + \dots + W_{nn}
  \end{bmatrix}}^{D}
\end{equation}

\end{document}
Werner
  • 603,163
  • 1
    The math doesn't make sense though... – Werner Sep 15 '19 at 18:05
  • Thats awesome! its looks better than what I was expecting. How would I reference this though in a document? The math is from a Natural Language Processing model where we have many documents and train a model to output vectors for each word. Once we have these vectors we average them to obtain a single vector for each document. Some better explanation here: http://yaronvazana.com/2018/09/20/average-word-vectors-generate-document-paragraph-sentence-embeddings/ – user113156 Sep 15 '19 at 18:11
  • The usual method for referencing such a structure is to give it an equation number. Instead of using \[ ... \], wrap it in an equation environment, with a \label{...} inside. – barbara beeton Sep 16 '19 at 00:37
  • @user113156: See the updated answer. – Werner Sep 16 '19 at 05:38
6

A proposition with the mmatrix environment (medium size matrix) from nccmath and cellspace for a proper row spacing:

\documentclass{article}
\usepackage{amsmath, nccmath}

\usepackage[math]{cellspace}
\setlength{\cellspacetoplimit}{2pt}
\setlength{\cellspacebottomlimit}{2pt}

\newenvironment{bmmatrix}{\begin{bmatrix} \begin{mmatrix}}{\end{mmatrix}\end{bmatrix}}
\begin{document}

\begin{alignat}{3}
\begin{matrix}
W_1 \\
   \begin{bmmatrix}
           W_{11} \\
           W_{12} \\[-1.5ex]
           \vdots \\
           W_{1n}
   \end{bmmatrix}
\end{matrix} &+ &
\begin{matrix}
W_2 \\
\begin{bmmatrix}
           W_{21} \\
           W_{22} \\[-1.5ex]
           \vdots \\
           W_{2n}
       \end{bmmatrix}
\end{matrix}
   &+ \hdots{}
   &
+ \begin{matrix}
W_n \\
\begin{bmmatrix}
           W_{n1} \\
           W_{n2} \\[-1.5ex]
           \vdots \\
           W_{nn}
\end{bmmatrix}
\end{matrix}
   &=
\begin{matrix}
D \\
\begin{bmatrix}
           \mfrac{W_{11} + W_{21} + \hdots + W_{n1}}{n} \\[-1ex]
          \smash[b]{ \vdots }\\[-0.5ex]
           \mfrac{W_{1n} + W_{2n} + \hdots + W_{nn}}{n}
    \end{bmatrix}
\end{matrix}
\end{alignat}

\end{document} 

enter image description here

Bernard
  • 271,350
5

You can make use of the \overset command from the amsmath package; this lines up your code with the intended meaning, and avoids setting up any extra array structures for the contents of your equation.

If you've got a scalar factor next to a matrix (as suggested in another answer), then \overbrace makes it clear that the scalar factor is included within the annotated object.

Matrices annotated with \overset and \overbrace

\documentclass[10pt]{article}
\usepackage{amsmath} %maths

\begin{document}

\begin{equation}
    \overset{W_{1}}{
      \begin{bmatrix}
      W_{11} \\ W_{12} \\ \vdots \\ W_{1n}
    \end{bmatrix}}
%
    +
%
    \overset{W_{2}}{\begin{bmatrix}
      W_{21} \\ W_{22} \\ \vdots \\ W_{2n}
    \end{bmatrix}}
%
    +\dots+
%
    \overset{W_{n}}{\begin{bmatrix}
      W_{n1} \\ W_{n2} \\ \vdots \\ W_{nn}
    \end{bmatrix}}
%
    =
%
   \overbrace{ \dfrac{1}{n}
    \begin{bmatrix}
      W_{11} + W_{21} + \dots + W_{n1} \\
      W_{12} + W_{22} + \dots + W_{n2} \\
      \vdots \\
      W_{1n} + W_{2n} + \dots + W_{nn}
    \end{bmatrix}}^{D}
\end{equation}

\end{document}
RLH
  • 219
  • 1
  • 9
4

A solution with nicematrix:

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions{code-for-first-row = \scriptstyle}

\[
\begin{bNiceArray}{C}[first-row]
W_1 \\
W_{11} \\ 
W_{12} \\ 
\Vdots \\
W_{1n}
\end{bNiceArray}
+
\begin{bNiceArray}{C}[first-row]
W_1 \\
W_{21} \\ 
W_{22} \\ 
\Vdots \\
W_{2n}
\end{bNiceArray}
+
\cdots
+
\begin{bNiceArray}{C}[first-row]
W_n \\
W_{n1} \\ 
W_{n2} \\ 
\Vdots \\
W_{nn}
\end{bNiceArray}
=
\begin{bNiceArray}{C}[first-row]
D \\
\frac{W_{11}+W_{21}+\cdots+W_{n1}}{n} \\ 
\Vdots \\[5mm]
\frac{W_{1n}+W_{2n}+\cdots+W_{nn}}{n} 
\end{bNiceArray}
\]

\end{document}

Result of the above code

F. Pantigny
  • 40,250