[R] Extracting/setting elements from/in a matrix/array
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Mon Dec 31 14:51:02 CET 2001
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list