[R] adjacency matrix
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Thu Sep 21 15:51:34 CEST 2000
Mehdi Ghafariyan <mehdi_ghafariyan at yahoo.com> writes:
> Hi all;
> I have two vectors A=c(5,2,2,3,3,2)
> and B=c(2,3,4,5,6,1,3,2,4,3,1,5,1,4,6,1,4)
> and I want to make the following matrix
> using the information I have from the above
> vectors.
>
> 0 1 1 1 1 1
> 1 0 1 0 0 0
> 0 1 0 1 0 0
> 1 0 1 0 1 0
> 1 0 0 1 0 1
> 1 0 0 1 0 0
>
> so the first vector says that I have 6
> elements therefor I have to make a
> 6 by 6 matrix and then I have to read
> 5 elements from the second
> vector , and put 1 in [1,j] where j=2,3,4,5,6
> and put zero elsewhere( i.e. in [1,1])
> and so on.
> Any idea how this can be done in R ?
Yep. Matrix indices:
a<-c(5,2,2,3,3,2)
b<-c(2,3,4,5,6,1,3,2,4,3,1,5,1,4,6,1,4)
n<-length(a)
M<-matrix(0,n,n)
M[cbind(rep(1:n,a),b)]<-1
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list