Statalist The Stata Listserver


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

st: A Mata efficiency query


From   "Newson, Roger B" <[email protected]>
To   "statalist" <[email protected]>
Subject   st: A Mata efficiency query
Date   Mon, 10 Apr 2006 16:07:20 +0100

I have a query about efficient Mata programming. If I am iterating over
the rows and columns of a matrix X (say) and processing its elements one
by one (referring several times to each element), then do I save any
time by storing each element temporarily in a scalar at each iteration
(as I would have done in FORTRAN or C)? Or, as a Mata scalar is simply a
1x1 Mata matrix, does this in fact save no time at all? (I already know
that I shouldn't iterate over the rows and columns of a Mata matrix
unless there is no Mata expression or function capable of doing the
job.)

For instance, I might program:

for (i1=rows(X);i1>0;i1--) {
  for (i2=cols(X);i2>0;i2--) {
    Xi1i2=X[i1,i2]
    if (nonmissing(Xi1i2)) {
      X[i1,i2] = Xi1i2>1 ? . : min(Xi1i2,0.9999)
    }
  }
}

Is this more or less efficient than programming

for (i1=rows(X);i1>0;i1--) {
  for (i2=cols(X);i2>0;i2--) {
    if (nonmissing(X[i1,i2])) {
      X[i1,i2] = X[i1,i2]>1 ? . : min(X[i1,i2],0.9999)
    }
  }
}

which should have the same end result (I think)?

Roger

Roger Newson
Lecturer in Medical Statistics
POSTAL ADDRESS:
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute at Imperial College London
St Mary's Campus
Norfolk Place
London W2 1PG
STREET ADDRESS:
Respiratory Epidemiology and Public Health Group
National Heart and Lung Institute at Imperial College London
47 Praed Street
Paddington
London W1 1NR
TELEPHONE: (+44) 020 7594 0939
FAX: (+44) 020 7594 0942
EMAIL: [email protected]
WEBSITE: http://www.imperial.ac.uk/nhli/r.newson/
Opinions expressed are those of the author, not of the institution.


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