Statalist The Stata Listserver


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

Re: st: right shift elements in a matrix


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: right shift elements in a matrix
Date   Thu, 18 May 2006 11:16:22 -0500

On May 18, 2006, at 9:55 AM, Lei Xuan wrote:
After logistic regression (-logit-), I have a parameter matrix e(b).
Let
matrix b=e(b)
N=e(df_m)+1 (number of independent variables plus 1)

we have
b[1,1] b[1,2] ... b[1,N]

I want to generate a new matrix with the same elements but different order
(shift one element right), that is,

b[1,N], b[1,1], b[1,2], ..., b[1, N-1]

Here is a Mata-based solution to your immediate question:


cap mata: mata drop shift_columns()
mata:
void shift_columns(string scalar old_mat, string scalar new_mat) {
real matrix m
real rowvector v

m = st_matrix(old_mat)
v = cols(m), (1..cols(m)-1)

st_matrix(new_mat, m[.,v])
st_matrixrowstripe(new_mat, st_matrixrowstripe(old_mat))
st_matrixcolstripe(new_mat, st_matrixcolstripe(old_mat)[v,.])
}
end

mat b = e(b)
mata: shift_columns("b", "foo")
mat li foo


Note that the first line is required if you put this into a do-file and run it multiple times within the same Stata session; without it, the second run would throw an error because the function shift_columns () has already been defined.

I presume that this step is just part of a larger problem you are trying to tackle. If so, depending upon what that problem is, this might not be the optimal solution.


-- Phil

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