[R] zero in alternate elements in alternate rows in matrix

Michelle Morters mm675 at hermes.cam.ac.uk
Sat May 30 17:56:17 CEST 2015


Hi - I'm trying to generate the (toy) matrix below in R. I get the error 
message shown below (and no matrix!) and I'm unclear as to why - if 
someone could advise as to why, that would be ace. Thank you! Michelle


0.7 	0 	0.1 	0 	0.1 	0 	0 	0 	0 	0 	0 	0
0.1 	0.7 	0.1 	0.1 	0.1 	0.1 	0 	0 	0 	0 	0 	0
0.1 	0.1 	0.7 	0 	0.1 	0 	0.1 	0 	0 	0 	0 	0
0.1 	0.1 	0.1 	0.7 	0.1 	0.1 	0.1 	0.1 	0 	0 	0 	0
0.1 	0.1 	0.1 	0.1 	0.7 	0 	0.1 	0 	0.1 	0 	0 	0
0 	0.1 	0.1 	0.1 	0.1 	0.7 	0.1 	0.1 	0.1 	0.1 	0 	0
0 	0 	0.1 	0.1 	0.1 	0.1 	0.7 	0 	0.1 	0 	0.1 	0
0 	0 	0 	0.1 	0.1 	0.1 	0.1 	0.7 	0.1 	0.1 	0.1 	0.1
0 	0 	0 	0 	0.1 	0.1 	0.1 	0.1 	0.7 	0 	0.1 	0
0 	0 	0 	0 	0 	0.1 	0.1 	0.1 	0.1 	0.7 	0.1 	0.1
0 	0 	0 	0 	0 	0 	0.1 	0.1 	0.1 	0.1 	0.7 	0
0 	0 	0 	0 	0 	0 	0 	0.1 	0.1 	0.1 	0.1 	0.7



cross = 0.1
diag = 0.7
n=12  # to be changed to n=1:100 once the code below is correct
nbe=4
R0=sapply(n,function(x){
m = matrix(0, nrow=x, ncol=x)
for (i in 1:x)
{
for(j in 1:x)
{
if (i==j)
{
m[i,j] = diag
}
else if(j<=i+nbe & j>=i-nbe)
{
m[i,j]=cross
}
}
}
for (i in seq(1,x,2))
{
for (j in seq(i+1,i+nbe,2))
{
m[i,j]=0
}
}
print(m)
max(Mod(eigen(m)$value))
}
)
R0

error message: Error in `[<-`(`*tmp*`, i, j, value = 0) : subscript out 
of bounds










	[[alternative HTML version deleted]]



More information about the R-help mailing list