-1

I am working on array containing the objects. When I compare an element of two different arrays, it is giving an error as Error in cell_list[x4, 3, 1] : incorrect number of dimensions. The piece of code is as follows,

cnt2 <- 25
cell_list <- array(0, c(cnt2,6,1)) 
nbr_list <- array(0, c(25,6,1))
insert_nbrlist = function(nbr_list, cell_list, cnt1)
  {
    for(x3 in 1:cnt1)
      for(x4 in 1:cnt2)
      {
        if((nbr_list[x3,3,1] == cell_list[x4,3,1]) && (nbr_list[x3,4,1]== cell_list[x4,4,1]))
        {
          theta_diff <- cell_list[x4,1,1] - nbr_list[x3,1,1]
          phi_diff <- cell_list[x4,2,1] - nbr_list[x3,2,1]
        }
        else
        {
          cnt2 <- cnt2+1
          cell_list <- rbind(cell_list[,,1],nbr_list[x3,,1])
        }
    }
    return(cell_list)
  }

Here insert_nbrlist is a function called by the main program. Inner for loop will be executed once but for the second execution i.e. when x4= 2, it is giving me the error as Error in cell_list[x4, 3, 1] : incorrect number of dimensions. Can anybody help me please. Here cnt1 is passed to the function insert_nbrlist. cnt1 is the number of rows present in table1 of nbr_list and is constant. cnt2 represent the number of rows present in table1 of cell_list and it keeps on increasing. cnt2 I am not passing to the function because it is getting increased in that function. calling the function is not producing any error but the error is produced inside the function insert_nbrlist.

I want to put this question in another way. I have an array which is initialized to cell_list <- array (0, c(25,6,1]). In the program I am adding one more row to this table1 of cell_list. After adding a row to the array I am unable access any element from that array. I am getting the following error, Error in cell_list[x4, 3, 1] : incorrect number of dimensions. How to access an element from an array after adding a row to that array.

I am rewriting the code with the main program

func <- function()
{

  cnt2 <- 25

  insert_nbrlist = function(nbr_list, cell_list)
  {
    for(x3 in 1:25)
      for(x4 in 1:cnt2)
      {
        if((nbr_list[x3,3,1] == cell_list[x4,3,1]) && (nbr_list[x3,4,1]== cell_list[x4,4,1]))
        {
          theta_diff <- cell_list[x4,1,1] - nbr_list[x3,1,1]
          phi_diff <- cell_list[x4,2,1] - nbr_list[x3,2,1]
        }
        else
        {
          cnt2 <- cnt2+1
          cell_list <- rbind(cell_list[,,1],nbr_list[x3,,1])
        }
    }
    return(cell_list)
  }

  cnt <- 0
  m <- 50
  r1 <- 10 
  dtheta <- 0.5
  dphi <- 0.5

  cell_list <- array(0, c(cnt2,6,1)) 
  nbr_list <- array(0, c(25,6,1))

  repeat
  {
    cnt <- cnt+1
    s <- runif(2,-pi/2 : pi/2)

    theta <- s[1]
    phi <- s[2]
    cnt1 <- 0
    for(x1 in -2:2)
      for(x2 in -2:2)
      {
        cnt1 <- cnt1+1
        theta1 <- theta+x1*dtheta
        phi1 <- phi +x2*dphi

        x <- round(r1*(sin(theta1) + sin(theta1+phi1))+m/2, digits=0)
        y <- round(r1*(cos(theta1)+ cos(theta1+phi1))+m/2,digits=0)

        if((theta1 == theta) && (phi1 == phi))
          nbr_list[cnt1,,1] <- c(theta1, phi1, x, y, 1,1)
        else
          nbr_list[cnt1,,1] <- c(theta1, phi1, x, y, 1,0)
      }

      if(cnt==1)
        cell_list <- nbr_list

      else
       cell_list <- insert_nbrlist(nbr_list, cell_list)

      if(cnt == 50)
        break

  }

}    

func() 

I would like to add simple code.

test<- function()
{
  sink("G:/rvma/test/test",append=FALSE) 
  nbr_list <- array(0, c(5,5,1))
  nbr_list[1,,1] <- 15
  nbr_list[2,,1] <- -15
  print(nbr_list)
  n <- c(1,2,3,4,5)
  nbr_list <- rbind(nbr_list[,,1],n)
  print(dim(nbr_list))
  print(nbr_list)
  print(nbr_list[,,1]) 
  sink()
}    

I am able to access elements from an array before rbind. After rbind the dimension of the array is changed from 5X5X1 to 6X5X1, I am able to print the array if i specify print(nbr_list) but I am unable to print the same array if i specify print(nbr_list[,,1]), Here I am getting the error Error in nbr_list[, , 1] : incorrect number of dimensions. Even I am unable to access an element after rbind.

Aparanji
  • 23
  • 1
  • 5
  • What is `cnt1`? And why isn't it passed to `insert_nbrlist`? And is your call of the function, producing the error `insert_nbrlist(nbr_list, cell_list, cnt1, ctn2)`? – Qaswed Aug 04 '16 at 13:57
  • 1
    Please read: [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – catastrophic-failure Aug 04 '16 at 15:32
  • @aichao 1. Right now I am using one-dimensional array of 25 rows and 6 columns. Later number of rows of this 1st table will be increased. 2. Later on even i want to use multi-dimensional array – Aparanji Aug 04 '16 at 22:14
  • *a one-dimensional array of 25 rows and 6 columns* - no, that would be a two-dimensional array. (rows are the first dimension, columns are the second dimension). – Gregor Thomas Aug 04 '16 at 22:41

1 Answers1

1

My guess is your problem is this line in the else block:

cnt2 <- cnt2+1

this will push up the value of cnt2 beyond 25, and since it's a 25x6x1 array, it'll be out-of-bounds.

Edit

I suspect rbind isn't working right on a 3-D array. There is a package, abind which you could use instead of rbind.

More on this answer.

Community
  • 1
  • 1
Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
  • ah, maybe not. Just seen the rbind. Unless the rbind is binding to the wrong dimension? – Steve Cooper Aug 04 '16 at 22:53
  • Yes after rbind only I am facing this problem. After rbind, 26th row is created and the values are copied to 26th row. I am able print cell_list after rbind but unable to access an element from the array cell_list. I am strugling with this from past three days, I am unable to continue. Please someone help me – Aparanji Aug 05 '16 at 01:20