From: <Richard.Cotton_at_hsl.gov.uk>
Date: Wed, 19 Dec 2007 11:24:22 +0000

Try this:

{
dim1 <- dim(x1)
dim2 <- dim(x2)

seq1 <- list();for(i in 1:length(dim1)) seq1[[i]]=seq(dim1[i])     filter1 <- paste(seq1, collapse=",")     cmd1 <- paste("out[", filter1, "]", sep="")     seq2 <- list();for(i in 1:length(dim2)) seq2[[i]]=seq(dim2[i])     filter2 <- paste(seq2, collapse=",")     cmd2 <- paste("out[", filter2, "]", sep="")

out <- array(0, dim=pmax(dim1,dim2))     eval(parse(text=paste(cmd1, "<-", cmd1, "+ x1")))     eval(parse(text=paste(cmd2, "<-", cmd2, "+ x2")))     out
}

x1 <- array(round(5*runif(30)), dim=c(2,3,5)) x2 <- array(round(5*runif(24)), dim=c(3,4,2)) x <- x1 %add% x2

You probably want to add a check to make the dimensions the same, and the code needs tidying, but you get the idea.

Regards,
Richie.

Mathematical Sciences Unit
HSL r-help-bounces_at_r-project.org wrote on 19/12/2007 09:26:44:

> Hi
>
> suppose I have two arrays x1,x2 of dimensions a1,b1,c1 and
> a2,b2,c2 respectively.
>
> I want x = x1 "+" x2 with dimensions c(max(a1,a2), max(b1,b2),max
> (c1,c2))
>
> with
>
> x[a,b,c] = x1[a1,b1,c1] + x2[a2,b2,c2] if a <=min(a1,a2) , b<=min
> (b1,b2), c<=min(c1,c2)
>
> and the other bits either x1 or x2 or zero according to whether the
> coordinates
> are "in range" for x1 or x2 or neither.
>
> The answer has to work for arbitrary-dimensioned arrays.
>
> toy example follows (matrices):
>
>
> > x1
> [,1] [,2] [,3] [,4] [,5]
> [1,] 1 3 5 7 9
> [2,] 2 4 6 8 10
> > x2
> [,1] [,2] [,3]
> [1,] 1 4 7
> [2,] 2 5 8
> [3,] 3 6 9
> > x
> [,1] [,2] [,3] [,4] [,5]
> [1,] 2 7 12 7 9
> [2,] 4 9 14 8 10
> [3,] 3 6 9 0 0
> >
>
>
> Note the zeros at lower-right.
>
>
>
>
>
>
>
> --
> Robin Hankin
> Uncertainty Analyst and Neutral Theorist,
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
> tel 023-8059-7743
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}

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. Received on Wed 19 Dec 2007 - 11:28:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 19 Dec 2007 - 11:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.