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

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


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   st: RE: how to input a numlist into a matrix
Date   Fri, 6 Sep 2002 12:21:02 -0400

> -----Original Message-----
> From: Radu Ban [mailto:[email protected]] 
> Sent: Friday, September 06, 2002 12:16 PM
> To: [email protected]
> Subject: st: how to input a numlist into a matrix
> 
> 
> Dear all,
> 
> 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?
> 
> Thank you,
> Radu Ban

First, assign then numlist:

	local nlist "10(5)90 91(1)99 99.1(0.1)99.9 99.91(0.01)99.99"

Then, expand it and put it back in the local nlist

	numlist "`nlist'"
	local nlist `r(numlist)'

Then, find out the number of elements and create the matrix

	local nel : words of `nlist'
	matrix A = J(1,`nel',0)

And put the elements in:

	forval i=1/`nel' {
		local el : word `i' of `nlist'
		mat A[1,`i']=`el'
	}

Even easier, though, is not to create A first, just do:

	numlist "`nlist'"
	local nlist `r(numlist)'
	foreach el of local nlist {
		matrix A = nullmat(A),`el'
	}

Nick Winter
*
*   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