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

Re:st: how to input a numlist into a matrix


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   Re:st: how to input a numlist into a matrix
Date   Fri, 6 Sep 2002 17:32:50 +0100

Radu Ban <[email protected]> wrote

> For a program, I want to input a numerical list into a matrix.
> I want to store the list in a local so that I can modify it easily.
Namely,
> I'm looking at something like:
>
>     local nlist "10(5)90 91(1)99 99.1(0.1)99.9 99.91(0.01)99.99"
>
> but then, if I use:
>
>     tokenize "`nlist'"
>
> and try to see if i'm doing the right thing by:
>
>     di `1'
>
> i get an invalid syntax error.
> Furthermore, I'm not sure how to get the size of the numlist so that
I can
> initialize my matrix with:
>
>     matrix A = J(1, size_of_numlist, 0)
>
> Can anyone give me some suggestions?

First, -tokenize- does not know or care that your input is a numlist.
It just chops it blindly into pieces of text, parsing on spaces, as
you specified no alternative. After -tokenize- local macro 1
contains

10(5)90

which you could -display- but only by forcing the interpretation
as a string:

di "`1'"

But that's neither here nor there. What you want can be done first
through -numlist-

numlist "`nlist'"

which leaves behind the expanded numlist, so we can count elements

local nn : word count `r(numlist)'

and then initialise the matrix

mat A = J(1,`nn',0)

and then replace element by element

tokenize `r(numlist)'

forval i = 1/`nn' {
	mat A[1,`i'] = ``i''
}


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/



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