[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Re: st: Making a Matrix from Three Variables

From   "Glenn Goldsmith" <>
To   <>
Subject   Re: Re: st: Making a Matrix from Three Variables
Date   Sun, 10 May 2009 11:45:01 +0100


Good call on the missing values front.

The other thing to potentially be wary of is that missing values in your
rows or cols variables will cause the -sparse()- code to do odd things. A
possible fix is below, but it may be easiest just to ensure there aren't any
missing values in your initial x matrix.

real matrix sparse(real matrix x)
  real matrix y
  real scalar k

  y = J(colmax(x[,1]),colmax(x[,2]),.)
  for (k=1; k<=rows(x); k++) {
    if (x[k,1]!=. & x[k,2]!=.) y[x[k,1],x[k,2]] = x[k,3]


Tirthankar Chakravarty <> wrote:

Many apologies. Read Glenn for Allan.


On Sat, May 9, 2009 at 10:38 PM, Tirthankar Chakravarty
<> wrote:
> <>
> <>
> 1) Amadou's solution can be streamlined considerably by replacing the
> first part as in the code included below.
> 2) The line:
> tomata x y z, missing
> does not work because -missing- is not a valid option for -tomata- (Gould,
> 3) Allan's solution is efficient, but you might want to watch out for
> missing values in your original data. So, a slight modification is
> included below.

*   For searches and help try:

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