Statalist The Stata Listserver


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

RE: st: right shift elements in a matrix (more)


From   "Feiveson, Alan H. (JSC-SK311)" <[email protected]>
To   <[email protected]>
Subject   RE: st: right shift elements in a matrix (more)
Date   Thu, 18 May 2006 12:38:36 -0500

After all that jazz about a permutation matrix, I just realized - why
not simply do

. matrix b1=b[1,4],b

. matrix bn=b1[.,1..4]

 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Feiveson,
Alan H. (JSC-SK311)
Sent: Thursday, May 18, 2006 12:29 PM
To: [email protected]
Subject: RE: st: right shift elements in a matrix

Here is a way to do it using a permutation matrix C (see example below
for N=4) . matrix b=(.1, .2, .3, .4)

. matrix list b

b[1,4]
    c1  c2  c3  c4
r1  .1  .2  .3  .4

. matrix A=I(4)

. matrix c=A[1..1,.]

. matrix B=A\c

. matrix list B

B[5,4]
    c1  c2  c3  c4
r1   1   0   0   0
r2   0   1   0   0
r3   0   0   1   0
r4   0   0   0   1
r1   1   0   0   0

. matrix C=B[2..5,.]

. matrix list C

C[4,4]
    c1  c2  c3  c4
r2   0   1   0   0
r3   0   0   1   0
r4   0   0   0   1
r1   1   0   0   0

. matrix bn=b*C

. matrix list bn

bn[1,4]
    c1  c2  c3  c4
r1  .4  .1  .2  .3



-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Phil Schumm
Sent: Thursday, May 18, 2006 11:16 AM
To: [email protected]
Subject: Re: st: right shift elements in a matrix

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/

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

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