Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: creating a matrix with unknown number of rows

Tags:

r

I have written the code below to generate a matrix containing what is, to me, a fairly complex pattern. In this case I determined that there are 136 rows in the finished matrix by trial and error.

I could write a function to calculate the number of matrix rows in advance, but the function would be a little complex. In this example the number of rows in the matrix = ((4 * 3 + 1) + (3 * 3 + 1) + (2 * 3 + 1) + (1 * 3 + 1)) * 4.

Is there an easy and efficient way to create matrices in R without hard-wiring the number of rows in the matrix statement? In other words, is there an easy way to let R simply add a row to a matrix as needed when using for-loops?

I have presented one solution that employs rbind at each pass through the loops, but that seems a little convoluted and I was wondering if there might be a much easier solution.

Sorry if this question is redundant with an earlier question. I could not locate a similar question using the search feature on this site or using an internet search engine today, although I think I have found a similar question somewhere in the past.

Below are 2 sets of example code, one using rbind and the other where I used trial and error to set nrow=136 in advance.

Thanks for any suggestions.

v1     <- 5
v2     <- 2
v3     <- 2
v4     <- (v1-1)

my.matrix <- matrix(0, nrow=136, ncol=(v1+4) )

i = 1

for(a in 1:v2) {
  for(b in 1:v3) {
    for(c in 1:v4) {
      for(d in (c+1):v1) {

        if(d == (c+1)) l.s = 4 
        else           l.s = 3

        for(e in 1:l.s) {

          my.matrix[i,c] = 1

            if(d == (c+1)) my.matrix[i,d]  = (e-1)
            else           my.matrix[i,d]  =  e

          my.matrix[i,(v1+1)] = a
          my.matrix[i,(v1+2)] = b
          my.matrix[i,(v1+3)] = c
          my.matrix[i,(v1+4)] = d

          i <- i + 1

        }
      }
    }
  }
}

my.matrix2 <- matrix(0, nrow=1, ncol=(v1+4) )
my.matrix3 <- matrix(0, nrow=1, ncol=(v1+4) )

i = 1

for(a in 1:v2) {
  for(b in 1:v3) {
    for(c in 1:v4) {
      for(d in (c+1):v1) {

        if(d == (c+1)) l.s = 4 
        else           l.s = 3

        for(e  in 1:l.s) {

          my.matrix2[1,c] = 1

          if(d == (c+1)) my.matrix2[1,d]  = (e-1)
          else           my.matrix2[1,d]  =  e

          my.matrix2[1,(v1+1)] = a
          my.matrix2[1,(v1+2)] = b
          my.matrix2[1,(v1+3)] = c
          my.matrix2[1,(v1+4)] = d

          i <- i+1

          if(i == 2) my.matrix3 <- my.matrix2
          else       my.matrix3 <- rbind(my.matrix3, my.matrix2)

          my.matrix2 <- matrix(0, nrow=1, ncol=(v1+4) )

        }
      }
    }
  }
}

all.equal(my.matrix, my.matrix3)
like image 563
Mark Miller Avatar asked Mar 04 '12 07:03

Mark Miller


People also ask

How do you create an empty matrix in R?

In R, one column is created by default for a matrix, therefore, to create a matrix without a column we can use ncol =0.

How do I create a custom matrix in R?

To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector. You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix. Note: By default, matrices are in column-wise order.

How do you represent an empty matrix?

A matrix having at least one dimension equal to zero is called an empty matrix. The simplest empty matrix is 0-by-0 in size. Examples of more complex matrices are those of dimension 0 -by- 5 or 10 -by- 0 -by- 20. To create a 0-by-0 matrix, use the square bracket operators with no value specified.

How do you set the number of rows in R?

The nrow() function in R programming R provides us nrow() function to get the rows for an object. That is, with nrow() function, we can easily detect and extract the number of rows present in an object that can be matrix, data frame or even a dataset.


2 Answers

If you have some upper bound on the size of the matrix, you can create a matrix large enough to hold all the data

my.matrix <- matrix(0, nrow=v1*v2*v3*v4*4, ncol=(v1+4) )

and truncate it at the end.

my.matrix <- my.matrix[1:(i-1),]
like image 177
Vincent Zoonekynd Avatar answered Oct 02 '22 00:10

Vincent Zoonekynd


This is the generic form to do it. You can adapt it to your problem

matrix <- NULL
for(...){
 ...
 matrix <- rbind(matriz,vector)
}

where vector contains the row elements

like image 42
Shehroz Avatar answered Oct 02 '22 01:10

Shehroz