Skip to content
Back to formatted view

Raw Message

Message-ID: <323fc5d1-7c6c-41a2-9b16-b2034d221a43@e1g2000pra.googlegroups.com>
Date: 2009-03-02T03:18:06Z
From: Andrew
Subject: Partial sum of a vector
In-Reply-To: <581851.83619.qm@web52104.mail.re2.yahoo.com>

perhaps this?

M <- dim(data_m)[2]

for(j in 1:M){
    for (i in 4:T) {
        data_q[i-3,j]=sum(data_m[(i-3):i,j])
    }
}

of course, you can vectorize this and speed it up significantly, but
there is something evil about premature optimization.

On Mar 2, 1:29?pm, Mohammad Sabr <mohammad_s... at yahoo.com> wrote:
> I am trying to run a loop where I can sum parts of a matrix. For example, I want at each step in the loop to sum the?the next?4-values of a vector. I tried to do the following but the result were wrong:
> ?
> for (i in 4:T) {
> data_q[i-3,]=sum(data_m[i-3,]:data_m[i,])}
>
> ?
> can anyone please direct me on what should I do to run this loop properly
> ?
> Thanks in advance
> ? ? ? ? [[alternative HTML version deleted]]
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.