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

David Winsemius dwinsemius at comcast.net
Sun May 31 04:15:36 CEST 2015


On May 30, 2015, at 8:56 AM, Michelle Morters wrote:

> 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
> 
> 

One can use loopless methods:

 cross = 0.1
 diag_val = 0.7
 n=12  
 nbe=4
 m = matrix(0, nrow=n, ncol=n)
 diag(m)=diag_val      # probably better not to use 'diag' as name of value.
 rm <- row(m); cm <- col(m)  # creates indicator matrices of same size
 m[abs(rm - cm) < 4 & rm != cm] <- cross  # Fill supra- and sub-diagonals.
 m[ cm %% 2 == 0 & rm %% 2 == 1 & rm < cm ] = 0 
   # Zero supra diags in odd rows and in even cols.
#-------------
> m
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]  0.7  0.0  0.1  0.0  0.0  0.0  0.0  0.0  0.0   0.0   0.0   0.0
 [2,]  0.1  0.7  0.1  0.1  0.1  0.0  0.0  0.0  0.0   0.0   0.0   0.0
 [3,]  0.1  0.1  0.7  0.0  0.1  0.0  0.0  0.0  0.0   0.0   0.0   0.0
 [4,]  0.1  0.1  0.1  0.7  0.1  0.1  0.1  0.0  0.0   0.0   0.0   0.0
 [5,]  0.0  0.1  0.1  0.1  0.7  0.0  0.1  0.0  0.0   0.0   0.0   0.0
 [6,]  0.0  0.0  0.1  0.1  0.1  0.7  0.1  0.1  0.1   0.0   0.0   0.0
 [7,]  0.0  0.0  0.0  0.1  0.1  0.1  0.7  0.0  0.1   0.0   0.0   0.0
 [8,]  0.0  0.0  0.0  0.0  0.1  0.1  0.1  0.7  0.1   0.1   0.1   0.0
 [9,]  0.0  0.0  0.0  0.0  0.0  0.1  0.1  0.1  0.7   0.0   0.1   0.0
[10,]  0.0  0.0  0.0  0.0  0.0  0.0  0.1  0.1  0.1   0.7   0.1   0.1
[11,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.1  0.1   0.1   0.7   0.0
[12,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.1   0.1   0.1   0.7

-- 
David

> 
> 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)  

# this would be creating a series of "m" objects at various values of n x n dimensions and then over-writing them.

> 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]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list