I don't see how you are getting the result you provide. > m [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 1 2 3 0 0 [5,] 1 2 3 0 0 [6,] 1 2 3 0 0 [7,] 1 2 3 0 0 [8,] 1 2 3 0 0 [9,] 1 2 3 4 0 [10,] 1 2 3 4 0 [11,] 1 2 3 4 5 [12,] 1 2 3 4 5 > t(sapply(1:8, function(x) diag(m[x:12, ]))) [,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 3 0 0 [3,] 0 2 3 0 0 [4,] 1 2 3 0 0 [5,] 1 2 3 0 0 [6,] 1 2 3 4 0 [7,] 1 2 3 4 5 [8,] 1 2 3 4 5
These are all of the diagonals from the 1st through 8th rows. The first 3 begin with 0 so we leave them out, but then we have 4th: 1, 2, 3; 5th: 1, 2, 3; 6th: 1, 2, 3, 4, etc so you must have some additional rule in mind to get your answer. ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jorge I Velez Sent: Tuesday, October 27, 2015 2:44 PM To: jim holtman Cc: R-help Subject: Re: [R] Extract entries from matrix Dear Jim, Thank you very much for your quick reply. I am sorry for the confusion it may have caused, but I messed up the indexes in my example. I would like, from the following matrix "m" ## input m <- structure(c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 0L, 0L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 4L, 4L, 4L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 5L, 5L), .Dim = c(12L, 5L)) to obtain 1 2 3 1 2 3 4 5 1 Sure using m[idx] will give the desired result. The problem is that idx is not known and needs to be determined from "m". I would like to use something like extractDiagonals(m) ## [1] 1 2 3 1 2 3 4 5 1 I look forward to your reply. Thanks in advance. Best regards, Jorge Velez.- On Tue, Oct 27, 2015 at 2:31 PM, jim holtman <jholt...@gmail.com> wrote: > If you want to use the numbers you gave a the index into the matrix, then > you can create a matrix with the values and then index into 'm'. I don't > see a '4' in the output example you gave using your index values: > > > m <- structure(c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > + 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, > + 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, > + 0L, 0L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, > + 3L, 3L, 3L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 4L, 4L, > + 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 0L, 0L, 0L, 0L, 0L, > + 0L, 0L, 0L, 0L, 0L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, > + 5L), .Dim = c(22L, 5L)) > > # create index matrix > > indx <- matrix(c(4, 1, > + 5, 2, > + 6, 3, > + 7, 1, > + 8, 2, > + 9, 3, > + 10, 1, > + 11, 2, > + 12, 3), ncol = 2, byrow = TRUE) > > > > > > m > [,1] [,2] [,3] [,4] [,5] > [1,] 0 0 0 0 0 > [2,] 0 0 0 0 0 > [3,] 0 0 0 0 0 > [4,] 1 2 3 0 0 > [5,] 1 2 3 0 0 > [6,] 1 2 3 0 0 > [7,] 1 2 3 0 0 > [8,] 1 2 3 0 0 > [9,] 1 2 3 4 0 > [10,] 1 2 3 4 0 > [11,] 1 2 3 4 5 > [12,] 1 2 3 4 5 > [13,] 1 2 3 4 5 > [14,] 1 2 3 4 5 > [15,] 1 2 3 4 5 > [16,] 1 2 3 4 5 > [17,] 1 2 3 4 5 > [18,] 1 2 3 4 5 > [19,] 1 2 3 4 5 > [20,] 1 2 3 4 5 > [21,] 1 2 3 4 5 > [22,] 1 2 3 4 5 > > indx > [,1] [,2] > [1,] 4 1 > [2,] 5 2 > [3,] 6 3 > [4,] 7 1 > [5,] 8 2 > [6,] 9 3 > [7,] 10 1 > [8,] 11 2 > [9,] 12 3 > > m[indx] > [1] 1 2 3 1 2 3 1 2 3 > > > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. > > On Tue, Oct 27, 2015 at 2:43 PM, Jorge I Velez <jorgeivanve...@gmail.com> > wrote: > >> Dear R-help, >> >> I am working with a matrix "m" from which I would like to extract some >> elements. An toy example is as follows: >> >> ## input matrix >> m <- structure(c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, >> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 2L, 2L, 2L, 2L, >> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, >> 0L, 0L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, >> 3L, 3L, 3L, 3L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 4L, 4L, >> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 0L, 0L, 0L, 0L, 0L, >> 0L, 0L, 0L, 0L, 0L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, >> 5L), .Dim = c(22L, 5L)) >> >> R> m >> # [,1] [,2] [,3] [,4] [,5] >> # [1,] 0 0 0 0 0 >> # [2,] 0 0 0 0 0 >> # [3,] 0 0 0 0 0 >> # [4,] 1 2 3 0 0 >> # [5,] 1 2 3 0 0 >> # [6,] 1 2 3 0 0 >> # [7,] 1 2 3 0 0 >> # [8,] 1 2 3 0 0 >> # [9,] 1 2 3 4 0 >> # [10,] 1 2 3 4 0 >> # [11,] 1 2 3 4 5 >> # [12,] 1 2 3 4 5 >> >> >From "m", I would like to extract the entries >> >> 4, 1 >> 5, 2 >> 6, 3 >> 7, 1 >> 8, 2 >> 9, 3 >> 10, 1 >> 11, 2 >> 12, 3 >> >> so at the end of applying a function "f" to "m" I get >> >> 1, 2, 3, 1, 2, 3, 4, 1, 2, 3 >> >> >> Basically the idea is to extract the diagonal elements until a zero is >> found. >> >> In the real problem the dimensions of "m" are much bigger, but this >> smaller >> version of "m" illustrate what needs to be done. >> >> I would greatly appreciate any ideas on how to do this. >> >> Thanks in advance, >> Jorge Velez.- >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> > > [[alternative HTML version deleted]] ______________________________________________ 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.