Re: [R] array addition

From: N. Lapidus <n.lapidus_at_gmail.com>
Date: Wed, 19 Dec 2007 11:29:16 +0100

Hi Robin,

Before someone gives a better solution, you can try this :

x1<-array(1:10,c(2,5))
x2<-array(1:9,c(3,3))

ArrayAdd<-function(array1,array2) {

x<-array(0,c(max(nrow(array1),nrow(array2)),max(ncol(array1),ncol(array2))))

x[1:nrow(array1),1:ncol(array1)]<-x[1:nrow(array1),1:ncol(array1)]+array1

x[1:nrow(array2),1:ncol(array2)]<-x[1:nrow(array2),1:ncol(array2)]+array2

    return(x)
}

> 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

> ArrayAdd(x1,x2)

     [,1] [,2] [,3] [,4] [,5]
[1,] 2 7 12 7 9
[2,] 4 9 14 8 10
[3,] 3 6 9 0 0

Nael

        [[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. Received on Wed 19 Dec 2007 - 10:32:49 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.