希望在数组上重复函数N次

时间:2019-03-13 19:48:36

标签: arrays r function

我有一个函数,将2d数组作为输入,并输出带有随机坐标翻转符号的数组。我想重复此功能n次,将其输出作为下一次的输入。我该怎么做?数组S是1 *和-1s随机排列的n * n数组

$c = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)
$c.open()

# THIS WORKS
$cmd = $c.CreateCommand()
$cmd.CommandText = "select * from TABLE where ID = 'MyID55'"
$reader = $cmd.ExecuteReader()
while ($reader.Read()) {
    for($i -eq 0; $i -lt $reader.FieldCount; $i++){
        $v = $reader.GetValue($i)
        if($v -ne ([System.DBNull]::Value)){
            "$($reader.GetName($i)): $v"
        }
    }
}
$reader.close()

# THIS RUNS, BUT DOESN'T RETURN VALUES
$cmd = $c.CreateCommand()
$cmd.CommandText = "select * from TABLE2 where ID = 'MyID55'"
$reader = $cmd.ExecuteReader()
while ($reader.Read()) {
    for($i -eq 0; $i -lt $reader.FieldCount; $i++){
        $v = $reader.GetValue($i)
        if($v -ne ([System.DBNull]::Value)){
            "$($reader.GetName($i)): $v"
        }
    }
}

$c.close()
$c.dispose()

1 个答案:

答案 0 :(得分:0)

也许您可以使用<<-将S添加到全局环境中,然后运行循环:

Thermal<-function(S,t=0.000000000001,k=1){
  #Defing beta
  beta<-1/(k*t)
  #multiplying each point of the array by all its adjacent points 
  #and summing them
  Spointenergy<-S*((S2)+(S3)+S4+S5)
  #Creating a loop over the whole array
    for(i in 1:n){
      for (j in 1:n){
        #defining the change in energy at each point
        dE<-energychange(S,i,j)
        #By default each point does not flip
        accept<-FALSE
        #If energy decreases spin flip occurs 100% of the time
        if (dE<0){
          accept<-TRUE
        }
        #If energy increases a spin flip will occur if w is greater than or equal
        #to u.
        if (dE>=0){
          w<-exp(-beta*dE)
          u<-runif(1,0,1)
          if (w>=u){ 
            accept<-TRUE
        }
        }
      #If no spin flip occurs there is no change in energy
      if (accept==FALSE){
        dE<-0
      }
      #if spin flip does occur then the magnitude of each lattice point is flipped
      #from positive to negative or visa versa
      if (accept==TRUE){ 
        S[i,j]<--S[i,j]
      }
      }
  }
  return(S)
  S<<-S
}

for (x in 1:10) {
  Thermal(S)
}
S

虹膜数据集的示例:

> iris_num <- iris[,-5]
> head(iris_num)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4
> 
> testfunction <- function(iris_num)
+ {
+   iris_num <<- iris_num + 1
+   }
> 
> testfunction(iris_num)
> head(iris_num)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          6.1         4.5          2.4         1.2
2          5.9         4.0          2.4         1.2
3          5.7         4.2          2.3         1.2
4          5.6         4.1          2.5         1.2
5          6.0         4.6          2.4         1.2
6          6.4         4.9          2.7         1.4
> 
> for (x in 1:10) {
+   testfunction(iris_num)
+ }
> 
> head(iris_num)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1         16.1        14.5         12.4        11.2
2         15.9        14.0         12.4        11.2
3         15.7        14.2         12.3        11.2
4         15.6        14.1         12.5        11.2
5         16.0        14.6         12.4        11.2
6         16.4        14.9         12.7        11.4
>