2016-07-01 11 views
-2

私はコーディングが新しく、この式をRのループに変換して、20x20の行列を与えたいと思います。の方程式をループに変換する

the equation:

私は

for (i in 1:20) { 
    for (j in 1:20) { 
     ws[i,] = exp((-1*(SpectData[i,]-SpectData[j,])^2)/(2*sigma^2)) 
    } 
} 

を試してみましたが、それはうまくいきませんでした、いずれかを助けることができますか?

+0

あなたはどのようなエラーを取得していますか? –

+0

SpectDataは行列またはベクトルですか? –

+0

サンプルデータ –

答えて

3

私は行列の各セルを、式の特定の値に割り当てたいと思っています。具体的な値は、ijです。もしそうなら、単に直接ws[i, j]を割り当てる:

ws <- matrix(, nrow = 20, ncol = 20) 

for (i in 1:20) { 
    for (j in 1:20) { 
     ws[i, j] = exp((-1*(SpectData[i,] - SpectData[j,])^2)/(2*sigma^2)) 
    } 
} 
+0

を投稿すると、次のようなエラーが表示されます。置き換えるアイテムの数が、置換の長さの倍数ではありません。何をすべきか ? –

+0

'class(SpectData [i、])°はあなたに何を表示しますか? –

3

をこれはouter()でベクトル化することができます。

w <- exp(-outer(x,x,`-`)^2/(2*sigma^2)); 
w; 
##   [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10]  [,11]  [,12]  [,13]  [,14]  [,15]  [,16]  [,17]  [,18]  [,19]  [,20] 
## [1,] 1.0000000 0.9985802 0.9882618 0.9496776 0.9994909 0.9511654 0.9439723 0.9806578 0.9836097 0.9948256 0.9995571 0.9990114 0.9780355 0.9982434 0.9687062 0.9932836 0.9747732 0.9361714 0.9983618 0.9677709 
## [2,] 0.9985802 1.0000000 0.9949761 0.9647143 0.9963753 0.9659729 0.9598513 0.9896375 0.9917785 0.9880335 0.9965552 0.9952306 0.9876813 0.9999821 0.9804218 0.9980308 0.9851899 0.9531183 0.9999922 0.9796737 
## [3,] 0.9882618 0.9949761 1.0000000 0.9860405 0.9829264 0.9868406 0.9828670 0.9990337 0.9996044 0.9678785 0.9833157 0.9805601 0.9983720 0.9955566 0.9951612 0.9992942 0.9973838 0.9782885 0.9953634 0.9947814 
## [4,] 0.9496776 0.9647143 0.9860405 1.0000000 0.9395095 0.9999880 0.9998338 0.9923777 0.9903106 0.9143392 0.9402200 0.9352755 0.9939033 0.9662471 0.9976097 0.9791557 0.9954698 0.9991247 0.9657302 0.9978649 
## [5,] 0.9994909 0.9963753 0.9829264 0.9395095 1.0000000 0.9411289 0.9333221 0.9739957 0.9774215 0.9975566 0.9999977 0.9999211 0.9709848 0.9958489 0.9604526 0.9891065 0.9672737 0.9249119 0.9960317 0.9594089 
## [6,] 0.9511654 0.9659729 0.9868406 0.9999880 0.9411289 1.0000000 0.9997322 0.9929686 0.9909773 0.9162297 0.9418306 0.9369453 0.9944311 0.9674793 0.9979365 0.9801309 0.9959236 0.9989074 0.9669713 0.9981732 
## [7,] 0.9439723 0.9598513 0.9828670 0.9998338 0.9333221 0.9997322 1.0000000 0.9899773 0.9876298 0.9071598 0.9340644 0.9289032 0.9917362 0.9614814 0.9961864 0.9753358 0.9935764 0.9997212 0.9609314 0.9965104 
## [8,] 0.9806578 0.9896375 0.9990337 0.9923777 0.9739957 0.9929686 0.9899773 1.0000000 0.9998745 0.9561391 0.9744734 0.9711142 0.9999140 0.9904757 0.9985148 0.9966804 0.9995965 0.9863894 0.9901949 0.9983006 
## [9,] 0.9836097 0.9917785 0.9996044 0.9903106 0.9774215 0.9909773 0.9876298 0.9998745 1.0000000 0.9605660 0.9778677 0.9747239 0.9995809 0.9925243 0.9975275 0.9978436 0.9990213 0.9836823 0.9922750 0.9972535 
## [10,] 0.9948256 0.9880335 0.9678785 0.9143392 0.9975566 0.9162297 0.9071598 0.9561391 0.9605660 1.0000000 0.9974046 0.9983548 0.9523096 0.9870979 0.9392555 0.9765274 0.9476553 0.8975016 0.9874195 0.9379853 
## [11,] 0.9995571 0.9965552 0.9833157 0.9402200 0.9999977 0.9418306 0.9340644 0.9744734 0.9778677 0.9974046 1.0000000 0.9998918 0.9714884 0.9960416 0.9610362 0.9894185 0.9678071 0.9256945 0.9962200 0.9599997 
## [12,] 0.9990114 0.9952306 0.9805601 0.9352755 0.9999211 0.9369453 0.9289032 0.9711142 0.9747239 0.9983548 0.9998918 1.0000000 0.9679528 0.9946300 0.9569555 0.9871912 0.9640680 0.9202597 0.9948379 0.9558700 
## [13,] 0.9780355 0.9876813 0.9983720 0.9939033 0.9709848 0.9944311 0.9917362 0.9999140 0.9995809 0.9523096 0.9714884 0.9679528 1.0000000 0.9885955 0.9991430 0.9955296 0.9998830 0.9884480 0.9882889 0.9989785 
## [14,] 0.9982434 0.9999821 0.9955566 0.9662471 0.9958489 0.9674793 0.9614814 0.9904757 0.9925243 0.9870979 0.9960416 0.9946300 0.9885955 1.0000000 0.9815727 0.9983883 0.9861922 0.9548720 0.9999979 0.9808461 
## [15,] 0.9687062 0.9804218 0.9951612 0.9976097 0.9604526 0.9979365 0.9961864 0.9985148 0.9975275 0.9392555 0.9610362 0.9569555 0.9991430 0.9815727 1.0000000 0.9907850 0.9996592 0.9938549 0.9811856 0.9999928 
## [16,] 0.9932836 0.9980308 0.9992942 0.9791557 0.9891065 0.9801309 0.9753358 0.9966804 0.9978436 0.9765274 0.9894185 0.9871912 0.9955296 0.9983883 0.9907850 1.0000000 0.9939727 0.9699313 0.9982709 0.9902655 
## [17,] 0.9747732 0.9851899 0.9973838 0.9954698 0.9672737 0.9959236 0.9935764 0.9995965 0.9990213 0.9476553 0.9678071 0.9640680 0.9998830 0.9861922 0.9996592 0.9939727 1.0000000 0.9906399 0.9858557 0.9995527 
## [18,] 0.9361714 0.9531183 0.9782885 0.9991247 0.9249119 0.9989074 0.9997212 0.9863894 0.9836823 0.8975016 0.9256945 0.9202597 0.9884480 0.9548720 0.9938549 0.9699313 0.9906399 1.0000000 0.9542799 0.9942673 
## [19,] 0.9983618 0.9999922 0.9953634 0.9657302 0.9960317 0.9669713 0.9609314 0.9901949 0.9922750 0.9874195 0.9962200 0.9948379 0.9882889 0.9999979 0.9811856 0.9982709 0.9858557 0.9542799 1.0000000 0.9804518 
## [20,] 0.9677709 0.9796737 0.9947814 0.9978649 0.9594089 0.9981732 0.9965104 0.9983006 0.9972535 0.9379853 0.9599997 0.9558700 0.9989785 0.9808461 0.9999928 0.9902655 0.9995527 0.9942673 0.9804518 1.0000000 

データ

set.seed(1L); 
N <- 20L; 
x <- runif(N); 
sigma <- 2; 
関連する問題