[R] translate vector of numbers to indicies of 0/1 matrix

2010-11-17 Thread Alexander Shenkin
Hello All, Searched around, haven't found a decent solution. I'd like to translate a vector of numbers to a matrix (or to a list of vectors) such that the vector values would serve as indicies of the 1's in an otherwise-zero-filled matrix (or list of vectors). For example: a = c(1,3,3,4) #

Re: [R] translate vector of numbers to indicies of 0/1 matrix

2010-11-17 Thread Phil Spector
Alexander - If I understand your problem, I think this function will make the matrix you want: makeyourmatrix = function(vec){ n = length(vec) mat = matrix(0,n,n) mat[cbind(1:n,vec)] = 1 mat } makeyourmatrix(c(1,3,3,4)) [,1] [,2] [,3] [,4] [1,]1000 [2,]

Re: [R] translate vector of numbers to indicies of 0/1 matrix

2010-11-17 Thread David Winsemius
On Nov 17, 2010, at 6:00 PM, Alexander Shenkin wrote: Hello All, Searched around, haven't found a decent solution. I'd like to translate a vector of numbers to a matrix (or to a list of vectors) such that the vector values would serve as indicies of the 1's in an otherwise-zero-filled

Re: [R] translate vector of numbers to indicies of 0/1 matrix

2010-11-17 Thread Gabor Grothendieck
On Wed, Nov 17, 2010 at 6:00 PM, Alexander Shenkin ashen...@ufl.edu wrote: Hello All, Searched around, haven't found a decent solution. I'd like to translate a vector of numbers to a matrix (or to a list of vectors) such that the vector values would serve as indicies of the 1's in an