Statalist


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

Re: st: How can I loop a FOR command across columns of a matrix


From   Mike Wazowski <[email protected]>
To   [email protected]
Subject   Re: st: How can I loop a FOR command across columns of a matrix
Date   Thu, 4 Jun 2009 21:33:03 -0700 (PDT)

in your case:

clear
input v1 v2 v3 v4 v5
2 3 5 10 6
1 2 8 13 6
end

xpose, clear
gen x = v1*v2
list

     +---------------+
     | v1   v2     x |
     |---------------|
  1. |  2    1     2 |
  2. |  3    2     6 |
  3. |  5    8    40 |
  4. | 10   13   130 |
  5. |  6    6    36 |
     +---------------+

if you need it back into a vector:

keep x
xpose, clear
list

     +-------------------------+
     | v1   v2   v3    v4   v5 |
     |-------------------------|
  1. |  2    6   40   130   36 |
     +-------------------------+







--- On Fri, 6/5/09, Nathan Danneman <[email protected]> wrote:

> From: Nathan Danneman <[email protected]>
> Subject: st: How can I loop a FOR command across columns of a matrix
> To: [email protected]
> Date: Friday, June 5, 2009, 2:44 AM
> Hello,
> 
> I have a 2 x N matrix, such as the following...
> 
> 2  3  5  10  6
> 1  2  8  13  6
> 
> I would like to write a loop, such as foreach or forvalues,
> that will
> take the first and second element of each column, and use
> them for
> some operation, and then move on to subsequent columns.
> 
> For examle,
> 
> foreach column i...
> multiply row a times row b.
> 
> Such that the above matrix would be turned into the vector
> 
> 2 6 40 130 36.
> 
> Is there a straightforward way to accomplish this without
> using Mata?
> 
> Thanks,
> Nathan
> 
> -- 
> "Imagination is more important than knowledge..."
>       -- Albert Einstein
> *
> *   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index