Dear all, suppose I have a vector with elements as: Vec <- c(2,3,4,5,6) Now I want to have all possible combination of length 3 using those elements and without any repetition. Like, I want to have 1 possibility like 2-3-4 but not 3-2-4. Can somebody guide me how to achieve that in R? Thanks for your help.
Perhaps this? t(combn(c(2,3,4,5,6), 3)) HTH, Jorge.- On Wed, Mar 28, 2012 at 3:17 PM, Somnath Dhar <> wrote:> Dear all, suppose I have a vector with elements as: > > Vec <- c(2,3,4,5,6) > > Now I want to have all possible combination of length 3 using those > elements and without any repetition. Like, I want to have 1 > possibility like 2-3-4 but not 3-2-4. > > Can somebody guide me how to achieve that in R? > > Thanks for your help. > > ______________________________________________ > R-help@r-project.org mailing list > 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]]
On 28-03-2012, at 21:17, Somnath Dhar wrote:> Dear all, suppose I have a vector with elements as: > > Vec <- c(2,3,4,5,6) > > Now I want to have all possible combination of length 3 using those > elements and without any repetition. Like, I want to have 1 > possibility like 2-3-4 but not 3-2-4. > > Can somebody guide me how to achieve that in R??combn Berend
Dear list-members, I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 etc. I also have a vector of length 3 (B). I want to construct a matrix of size 3x3 in the following way: - divide matrix A to 9 3x3 blocks - first is a11, a12, a13 a21, a22, a23 a31, a32, a33 - I want to get rowSums of this A1 matrix - Multiply A1*B and get a scalar, the first element of my new 3x3 matrix. I could do that with loop. Can you suggest something that is more elegant and faster? Thank you Daniel
On 28-Mar-2012 Kehl D?niel wrote:> Dear list-members, > I have a 9-by-9 matrix lets call it A with first row > a11, a12, a13,..., a19 etc. > I also have a vector of length 3 (B). > I want to construct a matrix of size 3x3 in the following way: > - divide matrix A to 9 3x3 blocks > - first is > a11, a12, a13 > a21, a22, a23 > a31, a32, a33 > - I want to get rowSums of this A1 matrix > - Multiply A1*B and get a scalar, the first element of my new 3x3 matrix. > > I could do that with loop. Can you suggest something that is more > elegant and faster? > > Thank you > DanielIt looks as though you want to do this for each of the 3x3 matrices. A possible solution could be on the following lines. For compactness I have reduced your problem to a 4x4 matrix A divided into 4 2x2 blocks. A: A11 A12 A13 A14 A21 A22 A23 A24 A31 A32 A33 A34 A41 A42 A43 A44 If you post-multiply the matrix A by the 4x2 matrix S: S: 1 0 1 0 0 1 0 1 then A%*%S gives you R11 R12 R21 R22 where Rij is the 2x1 matrix of row-sums of block (i,j) of A, e.g. R12: A13+A14 A23+A24 Similarly, if you have a vector B of length 2: B: b1 b2 and you set up the 4x2 matrix BB: BB: b1 0 b2 0 0 b1 0 b2 then A%*%BB is a 2x2 matrix BB11 BB12 BB21 BB22 where BBij is the result of {Block[i,j] of A}%*%B: A%*%BB: A11 A12 A13 A14 %*% b1 0 A21 A22 A23 A24 b2 0 A31 A32 A33 A34 0 b1 A41 A42 A43 A44 0 b2 E.g. BB12: A13*b1 + A14*b2 A23*b1 + A24*b2 This generalises immediately to your 9x9 and a 3-vector. Setting up the matrices S and BB should be straightforward (and once done can be applied to any matrix A). Does this help? ------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 28-Mar-2012 Time: 22:30:45 This message was sent by XFMail
see inline. On Wed, Mar 28, 2012 at 2:46 PM, Kehl D?niel <kehld at ktk.pte.hu> wrote:> Dear list-members, > > I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., a19 > etc. > I also have a vector of length 3 (B). > I want to construct a matrix of size 3x3 in the following way: > - divide matrix A to 9 3x3 blocks > - first is > ? ? ? ?a11, a12, a13 > ? ? ? ?a21, a22, a23 > ? ? ? ?a31, a32, a33 > - I want to get rowSums of this A1 matrixA %*% C, where C is a 9x3 matrix A <- matrix(1:81, 9, 9)> ones <- rep(1, 3) > zeros <- rep(0, 3) > C <- rbind(cbind(ones, zeros, zeros), cbind(zeros, ones, zeros), cbind(zeros, zeros, ones)) > Cones zeros zeros [1,] 1 0 0 [2,] 1 0 0 [3,] 1 0 0 [4,] 0 1 0 [5,] 0 1 0 [6,] 0 1 0 [7,] 0 0 1 [8,] 0 0 1 [9,] 0 0 1> A %*%Cones zeros zeros [1,] 30 111 192 [2,] 33 114 195 [3,] 36 117 198 [4,] 39 120 201 [5,] 42 123 204 [6,] 45 126 207 [7,] 48 129 210 [8,] 51 132 213 [9,] 54 135 216> - Multiply A1*B and get a scalar, the first element of my new 3x3 matrix.But from your description I get the new matrix will be 9x3, as above?> B <- 1:3 > A %*%C %*% B[,1] [1,] 828 [2,] 846 [3,] 864 [4,] 882 [5,] 900 [6,] 918 [7,] 936 [8,] 954 [9,] 972> (A %*%C %*% B)[1][1] 828 Kjetil> > I could do that with loop. Can you suggest something that is more elegant > and faster? > > Thank you > Daniel > > ______________________________________________ > R-help at r-project.org mailing list > 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.
On Wed, Mar 28, 2012 at 10:46:11PM +0200, Kehl D?niel wrote:> Dear list-members, > > I have a 9-by-9 matrix lets call it A with first row a11, a12, a13,..., > a19 etc. > I also have a vector of length 3 (B). > I want to construct a matrix of size 3x3 in the following way: > - divide matrix A to 9 3x3 blocks > - first is > a11, a12, a13 > a21, a22, a23 > a31, a32, a33 > - I want to get rowSums of this A1 matrix > - Multiply A1*B and get a scalar, the first element of my new 3x3 matrix.Hi. Try the following, which is based on the solution by Ted Harding. # some input A <- matrix(1:81, nrow=9, ncol=9) B <- 7:5 # compute the 3 x 3 matrix C <- diag(3)[rep(1:3, each=3), ] D <- cbind(rbind(B, 0, 0), rbind(0, B, 0), rbind(0, 0, B)) R1 <- D %*% A %*% C # compare with another approach E <- A * matrix(B, nrow=9, ncol=9) # component wise product C <- diag(3)[rep(1:3, each=3), ] R2 <- t(C) %*% E %*% C max(abs(R1 - R2)) # [1] 0 Hope this helps. Petr Savicky.