[R] Looking for a cleaner way to implement a setting certain indices of a matrix to 1 function

Anders Nielsen andersn at hawaii.edu
Tue May 8 23:55:27 CEST 2007


Hi Mark, 

Is this of any help?

  resMat<-function(K=6,lag=2,ncol=3*K){
    X<-matrix(0,K,ncol)
    X[,1:(K*lag)]<-diag(K)
    return(X)
  } 

Cheers, 

Anders. 

On Tuesday 08 May 2007 11:21 am, Leeds, Mark (IED) wrote:
> I wrote an ugly algorithm to set certain elements of a matrix to 1
> without looping and below works and you can see what
> The output is below the code.
> 
> K<-6
> lagnum<-2
> 
> restrictmat<-matrix(0,nrow=K,ncol=K*3)
> restrictmat[((col(restrictmat) - row(restrictmat) >= 0 ) &
> (col(restrictmat)-row(restrictmat)) %% K == 0)]<-1
> restrictmat[,(lagnum*K+1):ncol(restrictmat)]<-0
> 
>  restrictmat
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [,13] [,14] [,15] [,16] [,17] [,18]
> [1,]    1    0    0    0    0    0    1    0    0     0     0     0
> 0     0     0     0     0     0
> [2,]    0    1    0    0    0    0    0    1    0     0     0     0
> 0     0     0     0     0     0
> [3,]    0    0    1    0    0    0    0    0    1     0     0     0
> 0     0     0     0     0     0
> [4,]    0    0    0    1    0    0    0    0    0     1     0     0
> 0     0     0     0     0     0
> [5,]    0    0    0    0    1    0    0    0    0     0     1     0
> 0     0     0     0     0     0
> [6,]    0    0    0    0    0    1    0    0    0     0     0     1
> 0     0     0     0     0     0
> 
> For lagnum equals 1 , it also works :
> 
>  restrictmat
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [,13] [,14] [,15] [,16] [,17] [,18]
> [1,]    1    0    0    0    0    0    0    0    0     0     0     0
> 0     0     0     0     0     0
> [2,]    0    1    0    0    0    0    0    0    0     0     0     0
> 0     0     0     0     0     0
> [3,]    0    0    1    0    0    0    0    0    0     0     0     0
> 0     0     0     0     0     0
> [4,]    0    0    0    1    0    0    0    0    0     0     0     0
> 0     0     0     0     0     0
> [5,]    0    0    0    0    1    0    0    0    0     0     0     0
> 0     0     0     0     0     0
> [6,]    0    0    0    0    0    1    0    0    0     0     0     0
> 0     0     0     0     0     0
> 
> But I am thinking that there has to be a better way particularly because
> I'll get an error if I set lagnum to 3. 
> Any improvements or total revampings are appreciated. The number of
> columns will always be a multiple of the number of rows
> So K doesn't have to be 6. that was just to show what the commands do.
> thanks.
> --------------------------------------------------------
> 
> This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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.
>



More information about the R-help mailing list