[R] diag()<- in Matrix?

Jerome Goudet jerome.goudet at unil.ch
Sun Nov 5 16:08:33 CET 2006


Dear all,

I am trying to use the Matrix package to do some calculations on rather 
large and sparse matrices.  An example of such a matrix is given below. 

  mig<-0.2
  side<-10

  np<-side^2
  mig.mat<-matrix(0,np,np)
  diag(mig.mat[1:(np-side),(side+1):np])<-mig/4
  diag(mig.mat[(side+1):np,1:(np-side)])<-mig/4
  diag(mig.mat[-np,-1])<-mig/4
  nomig<-which(1:(side^2-1)%%side==0)
  diag(mig.mat[-np,-1])[nomig]<-0
  diag(mig.mat[-1,-np])<-mig/4
  diag(mig.mat[-1,-np])[nomig]<-0
  diag(mig.mat)<-1-apply(mig.mat,2,sum)

Here, side is set to 10, but ideally, I'd like it to be 500. 

I tried to simply replace matrix by Matrix in the code above, but 
obviously, it did not work...
The error came from the line below:

  diag(mig.mat[1:(np-side),(side+1):np])<-mig/4

the diag part is ok, but it seems that I cannot assign values to this 
vector.

I would be very glad for any help on this, or on pointers on how to 
construct Matrices.

As I am not on the list, please could you answer to jerome.goudet at unil.ch?

Many thanks in advance.


R 2.4.0
Windows XP
Matrix package version 0.9975-3



-- 
Jérôme GOUDET
Dep. Ecology & Evolution
Biophore, UNIL-Sorge
UNIL-CH-1015 Lausanne
Switzerland
http://www.unil.ch/dee
http://www.unil.ch/popgen
Tel: +41 21 692 42 42    Fax: +41 21 692 42 65
Secr:+41 21 692 42 60 
jerome.goudet at unil.ch



More information about the R-help mailing list