cumsum in 3d arrays

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

cumsum in 3d arrays

zloncaric
Hello!

Is it posible to apply cumsum() along the 3rd dimension of 3D array?
Something like matrlab function - cumsum (A,dim) which returns the cumulative sum of the elements along the dimension of A specified by scalar dim.

Thanks in advance

Željka

Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

bbolker
zloncaric <zloncaric <at> biologija.unios.hr> writes:

> Is it posible to apply /cumsum()/ along the 3rd dimension of 3D array?
> Something like matrlab function - /cumsum (*A*,dim)/ which returns the
> cumulative sum of the elements along the dimension of *A* specified by
> scalar dim.

  Check out the combination of apply and cumsum.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

David Winsemius
In reply to this post by zloncaric

On Nov 26, 2011, at 9:32 AM, zloncaric wrote:

> Hello!
>
> Is it posible to apply /cumsum()/ along the 3rd dimension of 3D array?
> Something like matrlab function - /cumsum (*A*,dim)/ which returns the
> cumulative sum of the elements along the dimension of *A* specified by
> scalar dim.
>

`apply` lets you chose which dimension gets selected.

Perhaps:

apply(mat, 3, cumsum)

(This is pretty basic stuff so you should probably be reading or at  
least skimming somewhat more thoroughly than you have so far the  
Introduction to R document and there is also the R for Matlab document  
by Bob Muenchen ... and a compendium of equivalencies by Hiebeler at: www.math.umaine.edu/~hiebeler/comp/matlabR.html
  )

--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

David Winsemius

On Nov 26, 2011, at 11:24 AM, David Winsemius wrote:

>
> On Nov 26, 2011, at 9:32 AM, zloncaric wrote:
>
>> Hello!
>>
>> Is it posible to apply /cumsum()/ along the 3rd dimension of 3D  
>> array?
>> Something like matrlab function - /cumsum (*A*,dim)/ which returns  
>> the
>> cumulative sum of the elements along the dimension of *A* specified  
>> by
>> scalar dim.
>>
>
> `apply` lets you chose which dimension gets selected.
>
> Perhaps:
>
> apply(mat, 3, cumsum)

Or perhaps

  apply(mat, 1:2, cumsum)

>
> (This is pretty basic stuff so you should probably be reading or at  
> least skimming somewhat more thoroughly than you have so far the  
> Introduction to R document and there is also the R for Matlab  
> document by Bob Muenchen ... and a compendium of equivalencies by  
> Hiebeler at: www.math.umaine.edu/~hiebeler/comp/matlabR.html )
>
> --

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

zloncaric
Thank you for your time and help.

I'm quite aware that this problem seems as basic stuff, and of course I've read several R and matlab manuals, and also consulted the D. Hiebeler, Matlab / R Reference and several others, but none of the suggested solutions seems to give me the right result.
I've tried the combination of apply and cumsum but the result is wrong.



Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

djmuseR
Hi:

Could you supply a small reproducible example with the output that you
expect? For example, what output would you expect from the following:

a <- array(1:24, c(2, 2, 3))

?
Dennis

On Mon, Nov 28, 2011 at 12:32 AM, zloncaric
<[hidden email]> wrote:

> Thank you for your time and help.
>
> I'm quite aware that this problem seems as basic stuff, and of course I've
> read several R and matlab manuals, and also consulted the D. Hiebeler,
> Matlab / R Reference and several others, but none of the suggested solutions
> seems to give me the right result.
> I've tried the combination of apply and cumsum but the result is wrong.
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/cumsum-in-3d-arrays-tp4110470p4114510.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

William Dunlap
In reply to this post by zloncaric
Does the following do what you want?  You often
need to use aperm() to permute the dimensions of
apply()'s output.

> a <- array(2^(0:23), dim=c(2,3,4))
> aperm(apply(a, 1:2, cumsum), c(2,3,1))
, , 1

     [,1] [,2] [,3]
[1,]    1    4   16
[2,]    2    8   32

, , 2

     [,1] [,2] [,3]
[1,]   65  260 1040
[2,]  130  520 2080

, , 3

     [,1]  [,2]   [,3]
[1,] 4161 16644  66576
[2,] 8322 33288 133152

, , 4

       [,1]    [,2]    [,3]
[1,] 266305 1065220 4260880
[2,] 532610 2130440 8521760


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of zloncaric
> Sent: Saturday, November 26, 2011 6:32 AM
> To: [hidden email]
> Subject: [R] cumsum in 3d arrays
>
> Hello!
>
> Is it posible to apply /cumsum()/ along the 3rd dimension of 3D array?
> Something like matrlab function - /cumsum (*A*,dim)/ which returns the
> cumulative sum of the elements along the dimension of *A* specified by
> scalar dim.
>
> Thanks in advance
>
> Željka
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/cumsum-in-3d-arrays-tp4110470p4110470.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: cumsum in 3d arrays

zloncaric
Yes exactly what I want! Thank you very much for your help.