Home > Blockchain >  How to make nested for-loop do every permutation
How to make nested for-loop do every permutation

Time:09-27

Basic question, but unsure how to resolve based on other posts that use vectors of characters or other situations that don't fully enlighten my own simple problem.

I want to make a nested for loop to calculate all possible combinations of two equations (x and y) along two vectors, and store every single calculation.

Here is my code:

n_c = 1
m_c = 1
n_n = 1
m_n = 1
my_data_c = c()
my_data_n = c()
rho_c_store = c()
rho_n_store = c()

for(i in 1:10){
  for(j in 1:10){
    rho_c = i / 10
    rho_n = j / 10
    
    x = (rho_c*n_c)/m_c
    y = (rho_n*n_n)/m_n
    
    rho_c_store[i] = rho_c
    rho_n_store[j] = rho_n
    
    my_data_c[i] = x
    my_data_n[j] = y
    
    my_data = cbind(rho_c_store,rho_n_store,my_data_c,my_data_n)
  }
}
print(my_data)

The output I get is:

> print(my_data)
      rho_c_store rho_n_store my_data_c my_data_n
 [1,]         0.1         0.1       0.1       0.1
 [2,]         0.2         0.2       0.2       0.2
 [3,]         0.3         0.3       0.3       0.3
 [4,]         0.4         0.4       0.4       0.4
 [5,]         0.5         0.5       0.5       0.5
 [6,]         0.6         0.6       0.6       0.6
 [7,]         0.7         0.7       0.7       0.7
 [8,]         0.8         0.8       0.8       0.8
 [9,]         0.9         0.9       0.9       0.9
[10,]         1.0         1.0       1.0       1.0

However, the data I want is:

> print(my_data)
      rho_c_store rho_n_store my_data_c my_data_n
 [1,]         0.1         0.1       ?        ?
 [2,]         0.1         0.2       ?        ?
 [3,]         0.1         0.3       ?        ?
 [4,]         0.1         0.4       ?        ?
 [5,]         0.1         0.5       ?        ?
 [6,]         0.1         0.6       ?        ?
 [7,]         0.1         0.7       ?        ?
 [8,]         0.1         0.8       ?        ?
 [9,]         0.1         0.9       ?        ?
[10,]         0.1         1.0       ?        ?
[11,]         0.2         0.1       ?        ?
[12,]         0.2         0.2       ?        ?
[13,]         0.2         0.3       ?        ?
[14,]         0.2         0.4       ?        ?
[15,]         0.2         0.5       ?        ?

... etc

I know I could solve this in some way with grid.expand() and an apply() function (trying to figure that out in parallel), but I'm annoyed at my inability to solve this basic code setup.

Thanks!

CodePudding user response:

As the others have pointed out you overwrite your elements again and again - you do not actually assign to the row that you mean to. I've included the code that explicitly calculates the index - maybe this is clearer to you. I've also made a few corrections (e.g. you can take the final construction out of the loop).

n_c <- 1 # generally: use <- for assignment
m_c <- 1
n_n <- 1
m_n <- 1
my_data_c <- c() # better: pre-allocate as vector("numeric", 100)
my_data_n <- c()
rho_c_store <- c()
rho_n_store <- c()

for (i in 1:10) {
  # you can move this assignment to the outer loop
  rho_c <- i / 10
  x <- (rho_c * n_c)/m_c

  for (j in 1:10) {
    rho_n <- j / 10
    y <- (rho_n * n_n)/m_n

    index <- 10*(i-1)   j
    rho_c_store[index] <- rho_c
    rho_n_store[index] <- rho_n

    my_data_c[index] <- x
    my_data_n[index] <- y
  }
}

# cbind should be outside the for loop, you only want to build the final
# matrix once you've completed building the vectors
my_data <- cbind(rho_c_store, rho_n_store, my_data_c, my_data_n)

print(my_data)

Hope this helps

CodePudding user response:

The problem is, that you store the variables at the wrong place. In other words when you loop through the second for-loop your i has always the same value. So you store each result of x on the same place. That's why you have to create an index which goes from 1:100 (in your case), and not from 1:10 only. Hope you understand what I mean.

If I take your code with with some corrections it should look like this

n_c = 1
m_c = 1
n_n = 1
m_n = 1
my_data_c = c()
my_data_n = c()
rho_c_store = c()
rho_n_store = c()

iter = 10

for(i in 1:iter){
  sequence = seq(i*iter-iter 1,i*iter)
  for(j in 1:iter){
    index = sequence[j]
    
    rho_c = i / 10
    rho_n = j / 10
    
    x = (rho_c*n_c)/m_c
    y = (rho_n*n_n)/m_n
    
    rho_c_store[index] = rho_c
    rho_n_store[index] = rho_n
    
    my_data_c[index] = x
    my_data_n[index] = y
    
    my_data = cbind(rho_c_store,rho_n_store,my_data_c,my_data_n)
  }
}
print(my_data)

In my example my seqence is an altering index which goes from 1 to 10 when i = 1 and then from 11 to 20 and so on.

I hope this is what you meant.

  • Related