# RE: st: right shift elements in a matrix

 From "Nick Cox" To Subject RE: st: right shift elements in a matrix Date Thu, 18 May 2006 18:06:50 +0100

```I think we can all agree that -svmat-

Alternatively, consider some retro-chic
or retro-not-so-chic solutions.

For a row vector with 4 elements,

mat b = e(b)
mat b = b[1,4], b[1,1..3]

More generally,

mat b = e(b)
local R = rowsof(b)
local r = `R' - 1
mat b = b[1,`R'], b[1,1..`r']

Yet more generally, check out the ancient
-matselrc- published in STB-56 in 2000.

. search matselrc

-matselrc- fixes the column names as
well, which the interactive code
above does not do.

Nick
n.j.cox@durham.ac.uk

Phil Schumm

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

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