Create an Expanding Matrix in R -
os: linux centos 6.5 / centos 7
machine: "special" 2.5 terabytes of physical memory (yes, really)
programming language: r
problem: in r, practice (appears be) allocate before loop matrix big final result. in case, final matrix size undetermined @ initiation.
thus, cleanest way of creating expanding matrix in r?
a key point wanted avoid overhead of memory copy @ point of expansion. if way, ok not preferred
important: familiar kernel level programming, kernel level memory management, page handling systems, etc. i new r, , learning r. (it interesting, , useful) in instance, trying create scenario demonstrate memory expansion of matrix without pre-allocation of memory, or fall allocation of size n matrix extensions (incremental expansion using memory chunks).
problem example
ok, in described scenario might create matrix a, create expanded matrix b, copy b perform operation on larger matrix.
an alternate approach expand a, populate result , iterate...
for example, might start using outer(), "outer()" creates new matrix @ each invocation. looking expand existing matrix...
x<- outer(1:2,1:2,"*") x [,1] [,2] [1,] 1 2 [2,] 2 4 ...
x<-outer(1:3,1:3,"*") x [,1] [,2] [,3] [1,] 1 2 3 [2,] 2 4 6 [3,] 3 6 9 ...
x<-outer(1:3,1:3,"*") x [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 2 4 6 8 [3,] 3 6 9 12 [4,] 4 8 12 16 hence, question:
"what preferred way create dynamically allocated matrix in r, without pre-allocation of large matrix?"
thanks in advance pointers , reading recommendations. appreciated.
in general, should allocate before loop matrix big final result , then, in each iteration, fill values. in example (if understood correctly how values filled), can try:
#set dimension of final matrix n<-10 #create matrix res<-matrix(ncol=n,nrow=n) #fill values res[1,1]<-1 (i in 2:n) { tmp<-c(i,(i+2):(2*i)) res[i,1:i]<-tmp res[1:i,i]<-tmp print(res) }
Comments
Post a Comment