Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: summing up matrix elements

From   "Jamie Griffin" <>
To   <>
Subject   Re: st: summing up matrix elements
Date   Tue, 08 Nov 2005 15:20:01 +0000

Assuming C is not too large, you can add up its elements like this:

matrix D=diag(vec(C))
local sum=(trace(D)-trace(C))/2

or if D would be too large for your Stata matsize, you can use:

matrix A=J(colsof(C), 1, 1)
matrix B=A'*C*A
local sum=(B[1,1]-trace(C))/2

See help matrix functions.

Jamie Griffin

>>> 11/08/05 2:47 pm >>>

Is there a way to to get a scalar containing the sum of the
elements of a pre-defined matrix?

For instance, define matrix C  as a correlation matrix. I want to know
is the sum of correlations between the various elements in C. Therefore
need the sum of all elements in C minus the sum of the diagonal
(-trace(C)-) and finally divide it by 2 (I am assuming matric C is 

Many thanks,

R.E. De Hoyos
Faculty of Economics
University of Cambridge
CB3 9DE, UK 
*   For searches and help try:
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index