简体   繁体   中英

for loops in r to create a matrix

enter image description here I want to create a 10 by 8 matrix from two matrices again with dimensions 10 by 8 by using for loops.

I have matrices a and e and I want to save the results of below code to a matrix. But when I run the code, the matrix chi is an empty matrix except with the last row of the last column. I am kind of newby to R, so any help is appreciated. Thanks.

chi <- matrix(nrow = 10, ncol = 8, byrow = T)
i <- nrow(a)
j <- ncol(a)
k <- nrow(e)
l <- ncol(e)
m <- nrow(chi)
n <- ncol(chi)
for (i in 1:nrow(a)) {
 for (j in 1:ncol(a)) {
   for (k in 1:nrow(e)) {
     for (l in 1:ncol(e))
    chi[m, n] <- ((a[i, j] - e[k, l]) ^ 2 / (e[k, l] * (1 - e[k, l])))
  }
 }

 }

Reconsider using any nested for loops as you can simply run matrix algebra since all inputs, a and e , are equal length objects:

chi <- ((a - e) ^ 2 / (e * (1 - e)))

With your nested for loop approach, your attempted matrix cell assignment is overwritten with each inner loop pass and only the very last instance is saved.

To demonstrate, consider the following random matrices (seeded for reproducibility):

set.seed(1162018)
a <- matrix(runif(800), nrow = 10, ncol = 8)
e <- matrix(runif(800), nrow = 10, ncol = 8)

With following output:

chi2 <- ((a - e) ^ 2 / (e * (1 - e)))
chi2
#               [,1]        [,2]        [,3]         [,4]       [,5]        [,6]         [,7]         [,8]
#  [1,]  1.090516287 5.314965506  0.30221649 4.3078030566 0.08185310  0.33991625 7.475638e-01 7.136321e+01
#  [2,]  0.339472596 0.037831564  1.00181544 0.0075194551 0.27228312 20.74823838 2.308509e-04 1.264312e-04
#  [3,]  0.001493967 0.009102797 17.76508355 0.0318190760 0.08133848  0.90538852 1.425952e-01 3.600838e-02
#  [4,] 25.941857200 2.182678801  0.52170472 0.5485710933 0.57015681  0.09332506 2.631002e-01 4.897862e-01
#  [5,]  4.341993499 0.075724451  0.03409925 0.0058830640 0.15290151  0.83227284 2.982630e+02 2.615268e-01
#  [6,]  0.327661207 0.058150213  0.17328257 0.3161902785 4.48620227  0.14685330 2.996204e+00 1.888419e+01
#  [7,]  0.456397833 1.446942556  0.51597191 0.2051742161 0.20440765  0.58169351 5.345522e+00 1.320896e-03
#  [8,] 12.844776005 0.753941152  0.36425134 0.0003481929 0.34011118  2.38649404 1.082046e-01 1.817180e-01
#  [9,]  0.042779101 0.119540004  1.41313002 0.1262586599 0.36583013  1.76476721 1.353301e+00 1.670491e-01
# [10,]  4.729182008 5.257386394  0.62181731 0.0000251250 0.32324943  0.08491841 6.627723e+00 2.127289e+00

Notice the very first, second, all the way to last elements of chi2 is consistent to your original formula as seen with using only single values. The all.equal() demonstrates no value difference between scientific notation or not.

((a[1, 1] - e[1, 1]) ^ 2 / (e[1, 1] * (1 - e[1, 1])))    
# [1] 1.090516

((a[1, 2] - e[1, 2]) ^ 2 / (e[1, 2] * (1 - e[1, 2])))
# [1] 1.090516
# ...
((a[10, 8] - e[10, 8]) ^ 2 / (e[10, 8] * (1 - e[10, 8])))
# [1] 2.127289

all.equal(2.127289e+00, 2.127289)
# [1] TRUE

Incorrect For Loop Processing

However, adjusting your for loop to use chi[i,j] assignment which does yield values but on closer look does not accurately align to your original formula:

chi <- matrix(nrow = 10, ncol = 8, byrow = T)

i <- nrow(a)
j <- ncol(a)
k <- nrow(e)
l <- ncol(e)
m <- nrow(chi)
n <- ncol(chi)
for (i in 1:nrow(a)) {
  for (j in 1:ncol(a)) {
    for (k in 1:nrow(e)) {
      for (l in 1:ncol(e))
        chi[i,j] <- ((a[i, j] - e[k, l]) ^ 2 / (e[k, l] * (1 - e[k, l])))          
    }
  }      
}

chi
#              [,1]       [,2]        [,3]       [,4]       [,5]        [,6]       [,7]      [,8]
#  [1,] 3.409875713 1.91797098 0.983457185 0.72023148 0.96731753 0.047236836 2.20811240 0.6073649
#  [2,] 0.011756997 2.96049899 3.614632753 1.30476270 2.49116488 0.074379894 1.01941080 0.3796867
#  [3,] 2.061628776 0.03227113 0.691592758 2.58226782 0.17603261 4.377353084 1.07957101 0.9584883
#  [4,] 5.477395731 0.07409188 5.287871705 1.86472765 2.02597697 0.078780553 6.20319269 2.6099405
#  [5,] 4.342937737 3.57579681 1.016981597 2.83351392 1.11431922 0.083484410 0.08412765 0.5525810
#  [6,] 0.008175703 2.63310577 0.005053893 3.69703754 0.05993078 0.004768071 5.92075341 4.2435415
#  [7,] 1.051921956 0.31217144 5.624012725 0.90161687 0.43301151 0.156739757 0.72284317 1.2243496
#  [8,] 4.941310521 4.85504735 0.021515999 3.66512027 0.08358373 3.603038468 0.38618455 6.1389345
#  [9,] 0.559136535 5.08204325 2.999036687 2.72726724 5.99168376 0.319859158 0.59398961 3.6221932
# [10,] 0.001668949 2.97353267 4.703763876 0.04979429 5.31715581 0.053267595 2.09966809 2.1272893

Here, the for loop returns only the very last instance since chi[i,j] is overwritten multiple times during loop. As a result, ALL elements of chi matrix uses the last element of e :

((a[1, 1] - e[10, 8]) ^ 2 / (e[10, 8] * (1 - e[10, 8])))
# [1] 3.409876

((a[1, 2] - e[10, 8]) ^ 2 / (e[10, 8] * (1 - e[10, 8])))
# [1] 1.917971
# ...
((a[10, 8] - e[10, 8]) ^ 2 / (e[10, 8] * (1 - e[10, 8])))
# [1] 2.127289

Conversely, with using chi[k,l] for assignment in loop.

for (i in 1:nrow(a)) {
  for (j in 1:ncol(a)) {
    for (k in 1:nrow(e)) {
      for (l in 1:ncol(e))
        chi[k,l] <- ((a[i, j] - e[k, l]) ^ 2 / (e[k, l] * (1 - e[k, l])))          
    }
  }    
}

chi
#               [,1]         [,2]         [,3]        [,4]        [,5]        [,6]         [,7]         [,8]
#  [1,] 5.649285e-01 5.813300e+00  0.035949545 10.14845208 0.002533313 0.405749651  0.711058301 2.592142e+01
#  [2,] 7.481556e+00 4.531135e-05  0.455696004  0.09284383 0.192074706 4.178867177  0.105489574 3.541626e-01
#  [3,] 4.953702e-04 6.703029e+00 41.109139456  0.08957573 1.511080005 0.254656165  0.004840752 2.805246e-01
#  [4,] 1.152237e+01 2.556255e-02  0.018652264  0.65975403 0.515919955 0.280219679  0.124379946 7.777978e-01
#  [5,] 2.126765e+00 5.356927e-01  0.251885418  0.06540162 0.008580900 0.003271672 41.259025738 2.963719e-06
#  [6,] 1.401345e-01 1.603721e-02  0.334385097  0.05865054 0.622973490 0.608273911  0.888928067 1.046868e+01
#  [7,] 1.018507e-01 1.756129e-01  0.005676374  0.72309875 0.011666290 0.314863595 12.420604213 7.778975e-02
#  [8,] 6.082752e+00 1.250805e-01  0.287099891  0.17209992 0.050136187 1.339028574  1.059674334 2.627769e-01
#  [9,] 8.005223e-02 9.260464e-02  2.823995704  0.04935770 0.020361815 0.258144647  0.275514317 9.392584e-03
# [10,] 4.952038e-01 3.870331e+00  0.089420009  1.05729955 0.002429084 0.349966871  6.702385325 2.127289e+00

As a result, ALL matrix elements uses the last values of a :

((a[10, 8] - e[1, 1]) ^ 2 / (e[1, 1] * (1 - e[1, 1])))
# [1] 0.5649285
all.equal(5.649285e-01, 0.5649285)
# [1] TRUE

((a[10, 8] - e[1, 2]) ^ 2 / (e[1, 2] * (1 - e[1, 2])))
# [1] 5.8133
all.equal(5.813300e+00, 5.8133)
# [1] TRUE
# ...
((a[10, 8] - e[10, 8]) ^ 2 / (e[10, 8] * (1 - e[10, 8])))
# [1] 2.127289

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM