Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <n.j.cox@durham.ac.uk> |

To |
"'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Mata looping question |

Date |
Tue, 18 Oct 2011 11:53:09 +0100 |

Here's a one-liner : b = a[|2,2\ .,.|] , J(rows(a) - 1, 1,.) \ J(1, cols(a), .) I imagine that there are neater ways to do it. Nick n.j.cox@durham.ac.uk -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: 18 October 2011 11:37 To: 'statalist@hsphsun2.harvard.edu' Subject: RE: st: Mata looping question Here's one way to do it. This could be compressed. As before, no loops are needed. : a = (1, 1 , 0\ 0, 2, 1\ 0 , 1, 0\0 , 0 , 0) : a 1 2 3 +-------------+ 1 | 1 1 0 | 2 | 0 2 1 | 3 | 0 1 0 | 4 | 0 0 0 | +-------------+ : b = a[|2,2\ .,.|] : b 1 2 +---------+ 1 | 2 1 | 2 | 1 0 | 3 | 0 0 | +---------+ : b , J(rows(b),1,.) 1 2 3 +-------------+ 1 | 2 1 . | 2 | 1 0 . | 3 | 0 0 . | +-------------+ : b \ J(1,cols(b),.) 1 2 +---------+ 1 | 2 1 | 2 | 1 0 | 3 | 0 0 | 4 | . . | +---------+ : b = b , J(rows(b),1,.) : b = b \ J(1,cols(b),.) : b 1 2 3 +-------------+ 1 | 2 1 . | 2 | 1 0 . | 3 | 0 0 . | 4 | . . . | +-------------+ Nick n.j.cox@durham.ac.uk Anna Reimondos Thanks very much for the reply that is very helpful..however I was hoping to shift the elements across the columns the other way if that makes sense. E.g. if matrix a is: c1 c2 c3 r1 1 0 0 r2 0 2 1 r3 0 1 0 r4 0 0 0 then matrix b I want to look like: c1 c2 c3 r1 2 1 . r2 1 0 . r3 0 0 . r4 . . . (To give a bit of context, the data I am working with has ages of women in the row e.g. 15, 16...45 and number of children at different ages e.g. 0,1,2,...15 in the columns . I am reverse ageing the data. So at r2,c2 it means in year T a woman aged 16 had 2 children aged 2. The b matrix is trying to get at year T-1. In that case she was 15 and had 2 children aged 1) On Tue, Oct 18, 2011 at 8:54 PM, Maarten Buis <maartenlbuis@gmail.com> wrote: > On Tue, Oct 18, 2011 at 11:37 AM, Anna Reimondos wrote: >> For example if I have one original matrix called 'a' >> >> matrix a=(1,0,0\0,2,1\0,1,0\0,0,0) >> matrix list a >> >> I then want to create another matrix 'b' which is equal to 'a' but >> where every element has been shifted up one row and across one column. > > I would not do that with a loop: > > *------------- begin example ---------------- > mata > a=(1,0,0\0,2,1\0,1,0\0,0,0) > b = J(5,1,0), (a \ J(1,3,0)) > b > end > *----------- end example ---------------- > (For more on examples I sent to the Statalist see: > http://www.maartenbuis.nl/example_faq ) > * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**st: Ask how copy values***From:*Adiwan Fahlan <adiwan_f@yahoo.com>

**Re: st: Mata looping question***From:*Anna Reimondos <areimondos@gmail.com>

**References**:**st: Mata looping question***From:*Anna Reimondos <areimondos@gmail.com>

**Re: st: Mata looping question***From:*Maarten Buis <maartenlbuis@gmail.com>

**Re: st: Mata looping question***From:*Anna Reimondos <areimondos@gmail.com>

**RE: st: Mata looping question***From:*Nick Cox <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: st: Mata looping question** - Next by Date:
**st: Comparing risk scores** - Previous by thread:
**RE: st: Mata looping question** - Next by thread:
**Re: st: Mata looping question** - Index(es):