【问题标题】:How to access an element of an array?如何访问数组的元素?
【发布时间】:2016-08-04 13:54:47
【问题描述】:

我正在处理包含对象的数组。当我比较两个不同数组的元素时,它给出一个错误,错误为 cell_list[x4, 3, 1] 中的错误:维数不正确。这段代码如下,

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)
  }

这里的insert_nbrlist是主程序调用的函数。内部 for 循环将执行一次,但对于第二次执行,即当 x4= 2 时,它给我的错误为 error in cell_list[x4, 3, 1] :不正确的维数。任何人都可以帮助我吗? 这里 cnt1 被传递给函数 insert_nbrlist。 cnt1 是 nbr_list 的 table1 中存在的行数,并且是常数。 cnt2 表示 cell_list 的 table1 中存在的行数,并且不断增加。 cnt2 我没有传递给该函数,因为它在该函数中正在增加。调用该函数不会产生任何错误,但该错误是在函数 insert_nbrlist 内部产生的。

我想以另一种方式提出这个问题。我有一个初始化为 cell_list

我正在用主程序重写代码

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() 

我想添加简单的代码。

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()
}    

我能够在 rbind 之前访问数组中的元素。在 rbind 之后,数组的维度从 5X5X1 更改为 6X5X1,如果我指定 print(nbr_list),我可以打印该数组,但如果我指定 print(nbr_list[,,1]),我将无法打印相同的数组,在这里,我收到错误 Error in nbr_list[, , 1] :不正确的维数。即使我在 rbind 之后也无法访问元素。

【问题讨论】:

  • 什么是cnt1?为什么不传递给insert_nbrlist?您是否调用该函数,产生错误insert_nbrlist(nbr_list, cell_list, cnt1, ctn2)
  • @aichao 1. 现在我使用的是 25 行 6 列的一维数组。稍后将增加第一个表的行数。 2. 稍后我什至想使用多维数组
  • 25 行 6 列的一维数组 - 不,那将是一个二维数组。 (行是第一个维度,列是第二个维度)。

标签: arrays r


【解决方案1】:

我猜你的问题是else 块中的这一行:

cnt2 <- cnt2+1

这会将cnt2 的值推高到 25 以上,并且由于它是一个 25x6x1 数组,因此会越界。

编辑

我怀疑rbind 在 3-D 阵列上无法正常工作。有一个包,abind,您可以使用它来代替rbind

更多on this answer

【讨论】:

  • 啊,也许不是。刚看到rbind。除非 rbind 绑定到错误的维度?
  • 是的,只有在 rbind 之后我才面临这个问题。在 rbind 之后,创建第 26 行并将值复制到第 26 行。我可以在 rbind 之后打印 cell_list,但无法访问数组 cell_list 中的元素。从过去的三天开始,我一直在为此苦苦挣扎,我无法继续。请有人帮助我
猜你喜欢
  • 1970-01-01
  • 2020-11-03
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
相关资源
最近更新 更多