s-news
[Top] [All Lists]

"cumsum" for matrices

To: <s-news@lists.biostat.wustl.edu>
Subject: "cumsum" for matrices
From: "Schwarz,Paul" <PSchwarz@gcrinsight.com>
Date: Fri, 20 Jan 2006 15:34:29 -0800
Thread-index: AcYeGg8NByCUH2GSR3GVoy7jVheYmg==
Thread-topic: "cumsum" for matrices
Dear S-News readers,

I'm working with 3D array objects created via tapply(), eg., tapply(vec,
list(x,y,z), sum). Using the returned array object, I want to create an
object that is the cumulative sum along the z dimension of the matrices
formed by x and y. So far, the best idea I've come up with is something
like the following, which involves a loop:

A <- array( rep(1:9, 3), c(3,3,3))  # test data array
cumsumA <- array(dim= dim(A))  # empty data array
for(z in 1:dim(A)[3]){cumsumA[,, z] <- apply(A[,, 1:z], c(1,2), sum)}


I suspect, however, that there is a better, more efficient way to do
this. Any suggestions?

Thanks for the help and advice.

-Paul

<Prev in Thread] Current Thread [Next in Thread>