Statalist The Stata Listserver


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

st: Mata subscripts on the left of an assignment


From   "Palmer, T.M." <[email protected]>
To   <[email protected]>
Subject   st: Mata subscripts on the left of an assignment
Date   Wed, 28 Feb 2007 15:26:12 -0000

Dear all,

I am having trouble following an example from the Mata Matters section
of the recent Stata Journal 7(1). It concerns the use of subscripts on
the left of an assignment to restore a vector to it's original order
using a permutation vector.

Here is some example code that restores a vector to its original order:

/* 1 */
mata
mata clear
y = (7\1\6\5)	
p = order(y,1)	
newy = y[p]		
y[p] = newy		
y
end

So y starts and finishes as,

: y
       1
    +-----+
  1 |  7  |
  2 |  1  |
  3 |  6  |
  4 |  5  |
    +-----+

In the article the following code on p112 is also claimed to work:

/* 2 */
mata
mata clear
y = (7\1\6\5)
p = order(y,1)
y = y[p]
y[p] = y
y
end

However y finishes as,

: y
       1
    +-----+
  1 |  1  |
  2 |  1  |
  3 |  6  |
  4 |  1  |
    +-----+

which is clearly not how it started.

Is Mata (incorrectly?) processing subscripts on the left of an
assignment on an element by element basis, the following code suggests
this is possible.

/* 3 */
mata
mata clear
y = (7\1\6\5)
p = order(y,1)
y = y[p]
for (i=1; i<=rows(y); i++) {
	y[p[i]] = y[i]
}
y
end


As an aside I note that if only y is to be referred to, then the
following code restores the sort order of y:

/* 4 */
mata
mata clear
y = (7\1\6\5)	
p = order(y,1)	
y = y[p]		
y = y[invorder(p)]		
y
end

Is Mata behaving incorrectly in code example 2 or is this my
misunderstanding?

I am using Intercooled Stata 9.2 for Windows, Born 17 May 2006.

Thanks in advance for any help,

Tom Palmer

PhD student
University of Leicester

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