Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Mata looping question


From   Anna Reimondos <areimondos@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Mata looping question
Date   Tue, 18 Oct 2011 22:35:24 +1100

That's perfect. Thank you so much!

On Tue, Oct 18, 2011 at 9:53 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> 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/
>

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


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index