Skip to content

help writing for loop

1 message · Steven McKinney

#
Hi,

The great thing about the S language is that it is 'vectorized',
so you can do a lot of matrix manipulations without loops.

Here's a smaller example of what you describe.
matrix A with 3 columns and 10 rows (instead of 100)
matrix B with 3 columns and 15 rows (instead of 1500)
[,1]       [,2]      [,3]
 [1,] 0.2875775 0.95683335 0.8895393
 [2,] 0.7883051 0.45333416 0.6928034
 [3,] 0.4089769 0.67757064 0.6405068
 [4,] 0.8830174 0.57263340 0.9942698
 [5,] 0.9404673 0.10292468 0.6557058
 [6,] 0.0455565 0.89982497 0.7085305
 [7,] 0.5281055 0.24608773 0.5440660
 [8,] 0.8924190 0.04205953 0.5941420
 [9,] 0.5514350 0.32792072 0.2891597
[10,] 0.4566147 0.95450365 0.1471136
x    
 [1,] 1  1   1
 [2,] 1  2   4
 [3,] 1  3   9
 [4,] 1  4  16
 [5,] 1  5  25
 [6,] 1  6  36
 [7,] 1  7  49
 [8,] 1  8  64
 [9,] 1  9  81
[10,] 1 10 100
[11,] 1 11 121
[12,] 1 12 144
[13,] 1 13 169
[14,] 1 14 196
[15,] 1 15 225
[,1]     [,2]      [,3]      [,4]      [,5]     [,6]     [,7]
 [1,] 2.133950 5.759401 11.163931 18.347540 27.310227 38.05199 50.57284
 [2,] 1.934443 4.466187  8.383538 13.686496 20.375061 28.44923 37.90901
 [3,] 1.727054 4.326145  8.206250 13.367368 19.809500 27.53265 36.53681
 [4,] 2.449921 6.005363 11.549346 19.081867 28.602929 40.11253 53.61067
 [5,] 1.699098 3.769140  7.150594 11.843459 17.847736 25.16342 33.79052
 [6,] 1.653912 4.679328  9.121806 14.981344 22.257943 30.95160 41.06232
 [7,] 1.318259 3.196545  6.162963 10.217513 15.360195 21.59101 28.90995
 [8,] 1.528621 3.353106  6.365876 10.566930 15.956267 22.53389 30.29979
 [9,] 1.168515 2.363915  4.137635  6.489674  9.420032 12.92871 17.01571
[10,] 1.558232 2.954077  4.644149  6.628448  8.906974 11.47973 14.34671
          [,8]     [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
 [1,] 64.87276 80.95176  98.80984 118.44700 139.86324 163.05856 188.03295
 [2,] 48.75440 60.98539  74.60199  89.60419 105.99201 123.76542 142.92445
 [3,] 46.82198 58.38816  71.23536  85.36358 100.77281 117.46305 135.43430
 [4,] 69.09735 86.57257 106.03633 127.48863 150.92947 176.35884 203.77676
 [5,] 43.72904 54.97896  67.54029  81.41304  96.59720 113.09277 130.89975
 [6,] 52.59011 65.53495  79.89685  95.67582 112.87184 131.48493 151.51508
 [7,] 37.31703 46.81224  57.39559  69.06706  81.82667  95.67440 110.61027
 [8,] 39.25398 49.39646  60.72722  73.24626  86.95358 101.84919 117.93309
 [9,] 21.68102 26.92466  32.74662  39.14689  46.12549  53.68240  61.81763
[10,] 17.50792 20.96335  24.71302  28.75691  33.09502  37.72737  42.65394
          [,15]
 [1,] 214.78642
 [2,] 163.46908
 [3,] 154.68657
 [4,] 233.18322
 [5,] 150.01814
 [6,] 172.96229
 [7,] 126.63428
 [8,] 135.20527
 [9,]  70.53119
[10,]  47.87474
so the results matrix mR has one column for each element of x,
and the i-th row element is the solution for
y = a + bx + cx^2
for the i-th row of coefficients in the parameter estimates matrix mA

You can spot check with e.g.
[,1]
[1,] 214.7864
[,1]
[1,] 27.31023
[1] 0.2875775 0.9568333 0.8895393
x    
 1  5 25
HTH

Steven McKinney, Ph.D.

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney +at+ bccrc +dot+ ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada