Stavros Macrakis
2011-Jun-13 21:34 UTC
[R] Composing two n-dimensional arrays into one n+1-dimensional array
If I have 2 n-dimensional arrays, how do I compose them into a n+1-dimension
array?
Is there a standard R function that's something like the following, but that
gives clean errors, handles all the edge cases, etc.
abind <- function(a,b) structure( c(a,b), dim = c(dim(a), 2) )
m1 <- array(1:6,c(2,3))
m2 <- m1 + 10
abind(m1,m2)
==>
, , 1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
, , 2
[,1] [,2] [,3]
[1,] 11 13 15
[2,] 12 14 16
Thanks,
-s
[[alternative HTML version deleted]]
Bert Gunter
2011-Jun-13 21:52 UTC
[R] Composing two n-dimensional arrays into one n+1-dimensional array
Strangely enough, abind() in the abind package !!
-- Bert
("all edge cases" is too vague to guess whether abind meets your
criteria)
On Mon, Jun 13, 2011 at 2:34 PM, Stavros Macrakis <macrakis at
alum.mit.edu> wrote:> If I have 2 n-dimensional arrays, how do I compose them into a
n+1-dimension
> array?
>
> Is there a standard R function that's something like the following, but
that
> gives clean errors, handles all the edge cases, etc.
>
> abind <- function(a,b) ?structure( c(a,b), dim = c(dim(a), 2) )
>
> m1 <- array(1:6,c(2,3))
> m2 <- m1 + 10
> abind(m1,m2)
>
> ==>
>
> , , 1
>
> ? ? [,1] [,2] [,3]
> [1,] ? ?1 ? ?3 ? ?5
> [2,] ? ?2 ? ?4 ? ?6
>
> , , 2
>
> ? ? [,1] [,2] [,3]
> [1,] ? 11 ? 13 ? 15
> [2,] ? 12 ? 14 ? 16
>
> Thanks,
>
> ? ? ? ? ? ? -s
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>
--
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."
-- Maimonides (1135-1204)
Bert Gunter
Genentech Nonclinical Biostatistics