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.