Here is another implementation:

> b
[1] 1 2 3 4 5
> c
[1] 1 2 1 3 5 4
> outer(c,b, "==")*1
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    0    0
[2,]    0    1    0    0    0
[3,]    1    0    0    0    0
[4,]    0    0    1    0    0
[5,]    0    0    0    0    1
[6,]    0    0    0    1    0

I hope this helps.

Chel Hee Lee

On 1/30/2015 11:52 AM, JS Huang wrote:
Hi,

   Here is my implementation.  Hope this helps.

b
[1] 1 2 3 4 5
c
[1] 1 2 1 3 5 4
sapply(b,function(x)ifelse(x==c,1,0))
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    0    0    0
[2,]    0    1    0    0    0
[3,]    1    0    0    0    0
[4,]    0    0    1    0    0
[5,]    0    0    0    0    1
[6,]    0    0    0    1    0



--
View this message in context: 
http://r.789695.n4.nabble.com/quetion-about-matrix-compute-tp4702505p4702532.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@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.


______________________________________________
R-help@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.

Reply via email to