Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Mata Display Matrix


From   "Ben Jann" <[email protected]>
To   [email protected]
Subject   Re: st: RE: Mata Display Matrix
Date   Tue, 28 Aug 2007 23:18:40 +0200

Marcello, here you go:

. mata:
------------------------------------------------- mata (type end to
exit) ---------------------
: x = uniform(4,4)

: matlist(x)          //=> uses the %9.0g format
                 1             2             3             4
    +---------------------------------------------------------+
  1 |   .399942225     .32823021    .186702991    .211767307  |
  2 |   .017106717    .947051507    .810609891    .980091133  |
  3 |   .178368227    .611467837    .091650288     .32447687  |
  4 |     .4022238    .508631968    .740951731    .082226228  |
    +---------------------------------------------------------+

: matlist(x,"%8.5f")
              1          2          3          4
    +---------------------------------------------+
  1 |   0.39994    0.32823    0.18670    0.21177  |
  2 |   0.01711    0.94705    0.81061    0.98009  |
  3 |   0.17837    0.61147    0.09165    0.32448  |
  4 |   0.40222    0.50863    0.74095    0.08223  |
    +---------------------------------------------+

: end

where -matlist()- is defined as follows:

/*----------------------------*/
*! version 1.0.0  28aug2007  Ben Jann
version 9.2
mata:
void matlist(
    real matrix X,
    | string scalar fmt
    )
{
    real scalar     i, j, wd, rw, cw
    string scalar   sfmt

    if (fmt=="") fmt = "%g"
    wd = strlen(sprintf(fmt,-1/3))

    if (length(X)==0) return

    rw = trunc(log10(rows(X))) + 1
    cw = trunc(log10(cols(X))) + 1
    wd = max((cw,wd)) + 2
    sfmt = "%"+strofreal(wd)+"s"

    printf("{txt}"+(2+rw+1+1)*" ")
    for (j=1;j<=cols(X);j++) {
        printf(sfmt+" ", sprintf("%g", j))
    }
    printf("  \n")
    printf((2+rw+1)*" " + "{c TLC}{hline " +
        strofreal((wd+1)*cols(X)+1) + "}{c TRC}\n")
    for (i=1;i<=rows(X);i++) {
        printf("{txt}  %"+strofreal(rw)+"s {c |}{res}", sprintf("%g", i))
        for (j=1;j<=cols(X);j++) {
            printf(sfmt+" ",sprintf(fmt, X[i,j]))
        }
        printf(" {txt}{c |}\n")
    }
    printf((2+rw+1)*" " + "{c BLC}{hline " +
        strofreal((wd+1)*cols(X)+1) + "}{c BRC}\n")
}
end
/*----------------------------*/

Note that -matlist()- does not split the table into pieces to fit the
window size and so it is a bad idea to use it with matrices that have
more than just a handful of columns. It would not  be a big deal to
implement the wrapping, but someone has to ask for it first.

Best,
ben

On 8/28/07, Marcello Pagano <[email protected]> wrote:
> I was hoping not to have to come back to Stata but rather stay in Mata,
> but that is what I am looking for.
> Thanks,
> Ben
>
> Ben Jann wrote:
> > I think Marcello means something like -matlist- for Mata matrices. I
> > don't know of any such implementation. However, an obvious workaround
> > would be
> >
> >  . mata
> >  : X = uniform(5,5)
> >  : st_matrix("X",X)
> >  : end
> >  . matlist X
> >
> > although this only works with a (non-void) real matrix.
> > ben
> >
> > On 8/28/07, Nick Cox <[email protected]> wrote:
> >
> >> Not quite clear on what the question is here, but
> >> I would look at -strofreal()-.
> >>
> >> Nick
> >> [email protected]
> >>
> >> Marcello Pagano
> >>
> >>
> >>> I know I can write the do-loop, but has anyone written one already to
> >>> display a matrix in Mata controlling the output format?
> >>>

>
*
*   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/



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