# [S] Q on a basic programming(2)

Mikio Kaihara (mkaihara@ichinoseki.ac.jp)
Thu, 04 Jun 1998 15:54:21 +0900

Dear Sir,

I would like to calculate the first derivative(ds) of the
first column and the second colume of the s(matrix(20x2)).
I used myh2(9,20,2). In this function, eigen(R)\$vectors are written below and I
confirmed the content of s was given to v. I did not fabricate s in three
dimension ? Please give me your advice (I wasted much time).

Best regards from the biginner of S-PLUS,
---------myh2(9,20,2)--------------------------------------------------
function(M, L, N)
{
NN <- N * N
LN <- L * N
LL <- L - 1
t <- matrix(rep(0, NN), ncol = N)
for(i in 1:N)
t[i, i] <- 1
v <- matrix(rep(0, LN), ncol = N)
s <- matrix(rep(0, LN), ncol = N)
v[, 1:N] <- eigen(R)\$vectors[, 1:N]
for(i in 1:N) {
for(k in 1:N) {
s[, i] <- s[, i] + t[i, k] * v[, k]
}
}
ds <- matrix(rep(0, LN), ncol = N)
for(j in 1:M) {
ds[1, j] <- s[2, j] - s[1, j]
ds[20, j] <- s[20, j] - s[19, j]
for(i in 2:LL) {
ds[i, j] <- (s[i + 1, j] - s[i - 1, j])/2
}
}
ds
}
--------------------------------------------------------------------------------
However, the result was below, but I cannot follow the error message..

> myh2(9,20,2)
Error in s[2, j]: Array subscript (3) out of bounds, should be at most 2
Dumped
>
--------------------------------------------------------------------------------
> eigen(R)\$vectors[,1]
[1] 0.211445283 0.239382870 0.264795541 0.286798772 0.304556467 0.317020269
0.322749805
[8] 0.320029031 0.307369155 0.284246538 0.251714540 0.212517850 0.170569181
0.129985312
[15] 0.094101950 0.064860046 0.042725061 0.027035258 0.016529704 0.009822227
> eigen(R)\$vectors[,2]
[1] 0.3288932533 0.3463575775 0.3394897780 0.3011848344 0.2281541960
0.1236765873
[7] -0.0009089761 -0.1273237543 -0.2345228730 -0.3047800875 -0.3292183438
-0.3102523765
[13] -0.2598419685 -0.1945605161 -0.1299309293 -0.0764404974 -0.0383597317
-0.0149486210
[19] -0.0027526076 0.0022719298

------------------------------
Mikio
mkaihara@ichinoseki.ac.jp
Tel:+81-191-24-4772、
Fax:+81-191-24-2146
INCT, 021-8511, Japan
------------------------------
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news