# RE: st: RE: matrix commands

 From "Nick Cox" <[email protected]> To <[email protected]> Subject RE: st: RE: matrix commands Date Mon, 7 Apr 2003 14:51:55 +0100

```Jesper Kuhl
> >
> > > I am trying to use some of STATA's matrix commands, but do
> > > not really succeed, and hope some of you can give me a hint.
> > >
> > > For my dataset I need a weighting factor (for every
> > > observation) calculated as
> > > w=xi'(X'X)^-1 *xi, where X is a n*k matrix of all variables
> > > and all observations, and xi is a k*1 vector with the
> > > variable-values for a single observation. The middle part,
> > > (X'X)^-1, I can build using "matrix accum", but I can't get
> > > STATA to build a n*1 vector for every observation to pre-
> > > and postmultiply it with. Hope some of you might a suggestion.
> >
>
> Thanks for your reply, Nick, but how do I use "mkmat" to
> build a vector for a single obs.? "mkmat" is set up to
> build vectors for every variable or a matrix for all
> variables, as far as I can see.

I was not reading carefully enough!

1. -xpose- first.

or

2.
Suppose you have ten variables
and you want observation 42 to go in a vector:

mat foo = J(10,1,1)
local i = 1
foreach v of var <varlist> {
mat foo[`i++',1] = `v'[42]
}

It sounds as if you need this within a loop:

mat foo = J(10,1,1)
forval j = 1 / `=_N' {
...
local i = 1
foreach v of var <varlist> {
mat foo[`i++',1] = `v'[`j']
}
...
}

Nick
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```