Laurent Gautier
2001-Dec-31 13:28 UTC
[R] Extracting/setting elements from/in a matrix/array
Dear all, I had to extracts/set elements from/in a matrix. Let say I have two vectors dim1 and dim2 of indices in the respective two dimensions of a matrix: I want to extract all the corresponding elements. I the case of a nxn matrix, dim1 <- 1:n and dim2 <- 1:n would extract the diagonal. I know one way would be to use the functions 'row' and 'col', but the matrixes I have are can be rather large. This is probably a detail but I was looking for something that would avoid be bit more memory friendly... I have made functions for that purpose but I suspect somebody already did something (which would be in R, but I could not find it...). For the sake of avoiding duplication of similar things (hence making code more readable), I would be happy to replace it in my code. Does anybody knows if such things already exists ? Laurent Laurent Gautier CBS, Building 208, DTU PhD. Student D-2800 Lyngby,Denmark tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2001-Dec-31 13:48 UTC
[R] Extracting/setting elements from/in a matrix/array
>>>>> "Laurent" == Laurent Gautier <laurent at genome.cbs.dtu.dk> writes:Laurent> Dear all, Laurent> I had to extracts/set elements from/in a Laurent> matrix. Let say I have two vectors dim1 and dim2 of Laurent> indices in the respective two dimensions of a Laurent> matrix: I want to extract all the corresponding Laurent> elements. I the case of a nxn matrix, dim1 <- 1:n Laurent> and dim2 <- 1:n would extract the diagonal. The solution to your problem has been part of the S language for ever : mat[ cbind(dim1, dim2) ] Note that this is mentioned in help("[") and MASS and ... Laurent> I know one way would be to use the functions 'row' Laurent> and 'col', but the matrixes I have are can be Laurent> rather large. This is probably a detail but I was Laurent> looking for something that would avoid be bit more Laurent> memory friendly... I have made functions for that Laurent> purpose but I suspect somebody already did Laurent> something (which would be in R, but I could not Laurent> find it...). For the sake of avoiding duplication Laurent> of similar things (hence making code more Laurent> readable), I would be happy to replace it in my Laurent> code. Laurent> Does anybody knows if such things already exists ? Laurent> Laurent Gautier CBS, Building 208, DTU PhD. Student Laurent> D-2800 Lyngby,Denmark tel: +45 45 25 24 85 Laurent> http://www.cbs.dtu.dk/laurent -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Dalgaard BSA
2001-Dec-31 13:51 UTC
[R] Extracting/setting elements from/in a matrix/array
Laurent Gautier <laurent at genome.cbs.dtu.dk> writes:> Dear all, > > > I had to extracts/set elements from/in a matrix. Let say I have two > vectors dim1 and dim2 of indices in the respective two dimensions of a > matrix: I want to extract all the corresponding elements. I the case of a > nxn matrix, dim1 <- 1:n and dim2 <- 1:n would extract the diagonal. > > I know one way would be to use the functions 'row' and 'col', but the > matrixes I have are can be rather large. This is probably a detail but > I was looking for something that would avoid be bit more memory > friendly... I have made functions for that purpose but I suspect somebody > already did something (which would be in R, but I could not find it...). > For the sake of avoiding duplication of similar things (hence making code > more readable), I would be happy to replace it in my code. > > Does anybody knows if such things already exists ?It does: A[cbind(dim1,dim2)] -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._