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: patterned matrix creation


From   Stas Kolenikov <skolenik@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: patterned matrix creation
Date   Fri, 10 Sep 2010 16:35:13 -0500

Tony,

learn Mata!

mata
n = 15
k = 3
A = J(n, n, 0)
for(i=1;i<=n;i++) {
  for(j=i;j<=i+k;j++) {
    A[i,mod(j-1,n)+1]=1
  }
}
A
st_matrix("A", A)
end

You can develop this into a stand alone function if needed.


On Fri, Sep 10, 2010 at 11:52 AM, Lachenbruch, Peter
<Peter.Lachenbruch@oregonstate.edu> wrote:
> Dear Statalisters,
>
> I want to create a matrix of the form in order to create running sums of 3 observations.  Note that the last 2 rows recycle times 1 and 2.  (This is to create sums of events in each of 12 months).
>
> M[12,12]
>      c1   c2   c3   c4   c5   c6   c7   c8   c9  c10  c11  c12
>  r1    1    1    1    0    0    0    0    0    0    0    0    0
>  r2    0    1    1    1    0    0    0    0    0    0    0    0
>  r3    0    0    1    1    1    0    0    0    0    0    0    0
>  r4    0    0    0    1    1    1    0    0    0    0    0    0
>  r5    0    0    0    0    1    1    1    0    0    0    0    0
>  r6    0    0    0    0    0    1    1    1    0    0    0    0
>  r7    0    0    0    0    0    0    1    1    1    0    0    0
>  r8    0    0    0    0    0    0    0    1    1    1    0    0
>  r9    0    0    0    0    0    0    0    0    1    1    1    0
> r10    0    0    0    0    0    0    0    0    0    1    1    1
> r11    1    0    0    0    0    0    0    0    0    0    1    1
> r12    1    1    0    0    0    0    0    0    0    0    0    1
>
> I entered this one manually - it's obviously prone to my own errors.  I would like to be able to do this easily with the matrix command J(r,s,z).  I can do this as a start with
> matrix A=(J(3,1,1)\J(9,1,0))'
> matrix A1=(0\J(3,1,1)\J(8,1,0))'
> and then concatenating.
> I'd like to write a simple foreach (or forval) loop for this.  I'm blanking on this - probably a result of old age and the beginning of the school year...
> Any quick ideas?
> It would be nice to be able to choose the sum length as 2 or 4 or 6, etc.
>
>
> Tony
>
> Peter A. Lachenbruch
> Department of Public Health
> Oregon State University
> Corvallis, OR 97330
> Phone: 541-737-3832
> FAX: 541-737-4001
>
>
>
> *
> *   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/
>



-- 
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

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