Statalist The Stata Listserver


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

st: Mata Structures


From   "Magdalena Luniak" <[email protected]>
To   [email protected]
Subject   st: Mata Structures
Date   Mon, 8 May 2006 17:36:50 +0200

hi all,

we try to  familiarize ourselves with the newly added Mata structures but we
got stuck. In the very end we would like program  "hashing" in order to make
searching more efficient. The kind of problem we have can be described as
follows.

We have defined the mata structure "point":

struct point {
 	real scalar a
 	real scalar b
}

and the function "help" that should return a vector of points. The
function is (or should be) defined such, that if it receives the real vector
seq =4,5 the return vector of point should be (4,4),(5,5).

The Mata Code for the function "help" looks as follows:

struct point vector function help(real vector seq) {
 	real scalar length
 	length = length(seq)
 	struct point vector v
 	v = point(length)
 	real scalar i
 	struct point scalar mypoint
 	for (i=1; i<=length; i++) {
 		mypoint.a=seq[i]
 		mypoint.b=seq[i]
 		liststruct(mypoint)
 		v[i] = mypoint
 		liststruct(v)
 	}
 	return(v)
}

Now, let's run this function by parsing the row-vector 4,5 to it:

: seq = 4,5
: help(seq)

Here is a commented log of the output: The output of -liststruct(mypoint)- in
the first iteration of the loop was:

1  structure of 2 elements
1.1  1 x 1 real = 4
1.2  1 x 1 real = 4
1  1 x 2 structure of 2 elements

and the output of -liststruct(v)- in the first iteration of the loop was:

    [1,1]:
1.1  1 x 1 real = 4
1.2  1 x 1 real = 4
1  [1,2]:
1.1  1 x 1 real = .
1.2  1 x 1 real = .

So far, this is the desired result. The problem arises in the second iteration
of the loop. The output of -liststruct(mypoint)- in the second iteration of
the loop was:

POINT2:  (5,5)
1  structure of 2 elements
1.1  1 x 1 real = 5
1.2  1 x 1 real = 5
1  1 x 2 structure of 2 elements

which again is what we expected. But the output of -liststruct(v)- was

   [1,1]:
1.1  1 x 1 real = 5
1.2  1 x 1 real = 5
1  [1,2]:
1.1  1 x 1 real = 5
1.2  1 x 1 real = 5

Although we had expected

   [1,1]:
1.1  1 x 1 real = 4
1.2  1 x 1 real = 4
1  [1,2]:
1.1  1 x 1 real = 5
1.2  1 x 1 real = 5

The problem is, that the first value of return vector is overwritten
by the value of the second element. We do not understand why?

Did we miss something obvious?

Many regards

Uli (and Magdalena Luniak)

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