Bookmark and Share

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]

RE: st: Mata looping question


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
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 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: 18 October 2011 11:37
To: '[email protected]'
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 
[email protected] 

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 <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index