[R] array addition
Robin Hankin
r.hankin at noc.soton.ac.uk
Fri Dec 21 09:55:08 CET 2007
[snip snip snip]
> 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))
[snip snip snip]
perhaps it wouldn't be too much to ask for you to
check the most recent version of the "magic" package?
[and we *really* don't want any whingeing about
magic_1.3-31 not being available. If I were you, I'd
email the package maintainer and tell him to release
updates in a more timely manner. . . ]
> library(magic)
> aplus
function (...)
{
args <- list(...)
if (length(args) == 1) {
return(args[[1]])
}
if (length(args) > 2) {
jj <- do.call("Recall", c(args[-1]))
return(do.call("Recall", c(list(args[[1]]), list(jj))))
}
a <- args[[1]]
b <- args[[2]]
dima <- dim(a)
dimb <- dim(b)
stopifnot(length(dima) == length(dimb))
out <- array(0, pmax(dima, dimb))
return(do.call("[<-", c(list(out), lapply(dima, seq_len),
list(a))) + do.call("[<-", c(list(out), lapply(dimb,
seq_len), list(b))))
}
>
--
Robin Hankin
Uncertainty Analyst and Neutral Theorist,
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
More information about the R-help
mailing list