OS: Linux Centos 6.5 / Centos 7
Machine: "Special" 2.5 Terabytes of Physical Memory (Yes, really)
Programming Language: R
Problem : In R, the practice (appears to be) to allocate before the loop a matrix as big as the final result. In this case, the final matrix size is undetermined at initiation.
Thus, what is the cleanest way of creating an expanding matrix in R?
A key point is that I wanted to avoid the overhead of a memory copy at the point of expansion. If that is the only way, that would be OK but not preferred
Important: I am very familiar with kernel level programming, kernel level memory management, page handling systems, etc. I am new to R, and learning R. (It is very interesting, and useful) In this instance, I am specifically trying to create a scenario to demonstrate memory expansion of a matrix without pre-allocation of memory, that or a fall back to allocation of size N matrix extensions (incremental expansion using memory chunks).
Problem Example
Ok, in the described scenario we might Create matrix A, create expanded matrix B, copy A to B then perform operation on larger matrix.
An alternate approach would be to expand A, populate result and iterate...
For example, we might start by using outer(), however "outer()" creates a new matrix at each invocation. I was looking to expand the 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, my question:
"What is the preferred way to create a dynamically allocated matrix in R, without pre-allocation of a large matrix?"
Thanks in advance to everyone for the pointers and reading recommendations. Your help is much appreciated.
In general, you should allocate before the loop a matrix as big as the final result and then, in each iteration, you fill the values. In your example (if I understood correctly how the values are filled), you can try:
#set the dimension of the final matrix
n<-10
#create the matrix
res<-matrix(ncol=n,nrow=n)
#fill the values
res[1,1]<-1
for (i in 2:n) {
tmp<-c(i,(i+2):(2*i))
res[i,1:i]<-tmp
res[1:i,i]<-tmp
print(res)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.