How to compute sigma in linear discriminant analysis

How to compute sigma in linear discriminant analysis?

The formula that I found in Elements of Statistical Learning (https://web.stanford.edu/~hastie/Papers/ESLII.pdf) on page 109 is somewhat ambiguous for me. According to it:

$$\hat \sum = \sum_{k=1}^K\sum_{g_i=k}(x_i - \hat u_k)(x_i-\hat u_k)^T/(N-K)$$

Now I don't understand what is $$g_i$$? For me it is just the $$i$$th categorical variable so must I compute the $$\sum_{g_i=k}(x_i - \hat u_k)(x_i-\hat u_k)^T$$ for every category where $$x_i$$ are the $$x$$'s given category $$k$$ and $$u_k$$ is its mean, then sum up the results and sum those for every category to get $$\hat \sum$$?

If it is so referring to this post (https://stackoverflow.com/questions/49331405/implement-linear-discriminant-analysis) how should one proceed with the computation of discriminant coefficients.

I tried to compute sigma using:

sigma <- cov(x[group1_index, ])+cov(x[group1_index, ])


then using the formula from the book:

But the matrices are not conformable because in this case the sigma is $$3$$ by $$3$$ and $$x^T$$ is $$3$$ by $$25$$