Re: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function
On Tuesday 08 May 2007 05:45:53 pm Leeds, Mark (IED) wrote: That's a good idea : I didn't realize that my matrices would look so bad in the final email. All I want To do is output 1's in the diagonal elements and zero's everywhere else but the matrix is not square so by diagonals I Really mean if Lagnum = 1 then the elements are (1,1), (2,2), (3,3),(4,4),(5,5),(6,6) Lagnum = 2 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6) Lagnum = 3 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6),(13,1),(1 4,2),(15,3),(16,4),(17,5), (18,6) And lagnum always has to be greater than or equal to 1 and less than or equal to (number of cols/number of rows ). Thanks For your advice. I think, the kronecker product method (by Gabor) is a cleaner solution. Something like: kronecker(matrix(1,1,Lagnum), diag(K)). My experience with such constructions, in really large dimensions, is that: kronecker(...) is much faster than {r,c}binds and rep(...). Regards PK __ R-help@stat.math.ethz.ch 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.
Re: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function
Suggestion: You might make it easier for folks to help if you explained in clear and simple terms what you are trying to do. Code is hard to deconstruct. Bert Gunter Genentech Nonclinical Statistics -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Tuesday, May 08, 2007 2:22 PM To: r-help@stat.math.ethz.ch Subject: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function I wrote an ugly algorithm to set certain elements of a matrix to 1 without looping and below works and you can see what The output is below the code. K-6 lagnum-2 restrictmat-matrix(0,nrow=K,ncol=K*3) restrictmat[((col(restrictmat) - row(restrictmat) = 0 ) (col(restrictmat)-row(restrictmat)) %% K == 0)]-1 restrictmat[,(lagnum*K+1):ncol(restrictmat)]-0 restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000100 0 0 0 0 0 0 0 0 0 [2,]010000010 0 0 0 0 0 0 0 0 0 [3,]001000001 0 0 0 0 0 0 0 0 0 [4,]000100000 1 0 0 0 0 0 0 0 0 [5,]000010000 0 1 0 0 0 0 0 0 0 [6,]000001000 0 0 1 0 0 0 0 0 0 For lagnum equals 1 , it also works : restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000000 0 0 0 0 0 0 0 0 0 [2,]010000000 0 0 0 0 0 0 0 0 0 [3,]001000000 0 0 0 0 0 0 0 0 0 [4,]000100000 0 0 0 0 0 0 0 0 0 [5,]000010000 0 0 0 0 0 0 0 0 0 [6,]000001000 0 0 0 0 0 0 0 0 0 But I am thinking that there has to be a better way particularly because I'll get an error if I set lagnum to 3. Any improvements or total revampings are appreciated. The number of columns will always be a multiple of the number of rows So K doesn't have to be 6. that was just to show what the commands do. thanks. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function
That's a good idea : I didn't realize that my matrices would look so bad in the final email. All I want To do is output 1's in the diagonal elements and zero's everywhere else but the matrix is not square so by diagonals I Really mean if Lagnum = 1 then the elements are (1,1), (2,2), (3,3),(4,4),(5,5),(6,6) Lagnum = 2 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6) Lagnum = 3 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6),(13,1),(1 4,2),(15,3),(16,4),(17,5), (18,6) And lagnum always has to be greater than or equal to 1 and less than or equal to (number of cols/number of rows ). Thanks For your advice. -Original Message- From: Bert Gunter [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 08, 2007 5:34 PM To: Leeds, Mark (IED); r-help@stat.math.ethz.ch Subject: RE: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function Suggestion: You might make it easier for folks to help if you explained in clear and simple terms what you are trying to do. Code is hard to deconstruct. Bert Gunter Genentech Nonclinical Statistics -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Tuesday, May 08, 2007 2:22 PM To: r-help@stat.math.ethz.ch Subject: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function I wrote an ugly algorithm to set certain elements of a matrix to 1 without looping and below works and you can see what The output is below the code. K-6 lagnum-2 restrictmat-matrix(0,nrow=K,ncol=K*3) restrictmat[((col(restrictmat) - row(restrictmat) = 0 ) (col(restrictmat)-row(restrictmat)) %% K == 0)]-1 restrictmat[,(lagnum*K+1):ncol(restrictmat)]-0 restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000100 0 0 0 0 0 0 0 0 0 [2,]010000010 0 0 0 0 0 0 0 0 0 [3,]001000001 0 0 0 0 0 0 0 0 0 [4,]000100000 1 0 0 0 0 0 0 0 0 [5,]000010000 0 1 0 0 0 0 0 0 0 [6,]000001000 0 0 1 0 0 0 0 0 0 For lagnum equals 1 , it also works : restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000000 0 0 0 0 0 0 0 0 0 [2,]010000000 0 0 0 0 0 0 0 0 0 [3,]001000000 0 0 0 0 0 0 0 0 0 [4,]000100000 0 0 0 0 0 0 0 0 0 [5,]000010000 0 0 0 0 0 0 0 0 0 [6,]000001000 0 0 0 0 0 0 0 0 0 But I am thinking that there has to be a better way particularly because I'll get an error if I set lagnum to 3. Any improvements or total revampings are appreciated. The number of columns will always be a multiple of the number of rows So K doesn't have to be 6. that was just to show what the commands do. thanks. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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.
Re: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function
Is this what you want? n - 6 lagnum - 2 result - NULL for (i in 1:lagnum) result - cbind(result, diag(n)) result [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [1,]100000100 0 0 0 [2,]010000010 0 0 0 [3,]001000001 0 0 0 [4,]000100000 1 0 0 [5,]000010000 0 1 0 [6,]000001000 0 0 1 On 5/8/07, Leeds, Mark (IED) [EMAIL PROTECTED] wrote: That's a good idea : I didn't realize that my matrices would look so bad in the final email. All I want To do is output 1's in the diagonal elements and zero's everywhere else but the matrix is not square so by diagonals I Really mean if Lagnum = 1 then the elements are (1,1), (2,2), (3,3),(4,4),(5,5),(6,6) Lagnum = 2 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6) Lagnum = 3 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6),(13,1),(1 4,2),(15,3),(16,4),(17,5), (18,6) And lagnum always has to be greater than or equal to 1 and less than or equal to (number of cols/number of rows ). Thanks For your advice. -Original Message- From: Bert Gunter [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 08, 2007 5:34 PM To: Leeds, Mark (IED); r-help@stat.math.ethz.ch Subject: RE: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function Suggestion: You might make it easier for folks to help if you explained in clear and simple terms what you are trying to do. Code is hard to deconstruct. Bert Gunter Genentech Nonclinical Statistics -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Tuesday, May 08, 2007 2:22 PM To: r-help@stat.math.ethz.ch Subject: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function I wrote an ugly algorithm to set certain elements of a matrix to 1 without looping and below works and you can see what The output is below the code. K-6 lagnum-2 restrictmat-matrix(0,nrow=K,ncol=K*3) restrictmat[((col(restrictmat) - row(restrictmat) = 0 ) (col(restrictmat)-row(restrictmat)) %% K == 0)]-1 restrictmat[,(lagnum*K+1):ncol(restrictmat)]-0 restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000100 0 0 0 0 0 0 0 0 0 [2,]010000010 0 0 0 0 0 0 0 0 0 [3,]001000001 0 0 0 0 0 0 0 0 0 [4,]000100000 1 0 0 0 0 0 0 0 0 [5,]000010000 0 1 0 0 0 0 0 0 0 [6,]000001000 0 0 1 0 0 0 0 0 0 For lagnum equals 1 , it also works : restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000000 0 0 0 0 0 0 0 0 0 [2,]010000000 0 0 0 0 0 0 0 0 0 [3,]001000000 0 0 0 0 0 0 0 0 0 [4,]000100000 0 0 0 0 0 0 0 0 0 [5,]000010000 0 0 0 0 0 0 0 0 0 [6,]000001000 0 0 0 0 0 0 0 0 0 But I am thinking that there has to be a better way particularly because I'll get an error if I set lagnum to 3. Any improvements or total revampings are appreciated. The number of columns will always be a multiple of the number of rows So K doesn't have to be 6. that was just to show what the commands do. thanks. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch mailing list
Re: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function
Try this: kronecker(t(rep(1:0, c(lagnum, 3-lagnum))), diag(K)) On 5/8/07, Leeds, Mark (IED) [EMAIL PROTECTED] wrote: That's a good idea : I didn't realize that my matrices would look so bad in the final email. All I want To do is output 1's in the diagonal elements and zero's everywhere else but the matrix is not square so by diagonals I Really mean if Lagnum = 1 then the elements are (1,1), (2,2), (3,3),(4,4),(5,5),(6,6) Lagnum = 2 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6) Lagnum = 3 then the elements (1,1), (2,2), (3,3),(4,4),(5,5),(6,6),(7,1),(8,2),(9,3),(10,4),(11,5),(12,6),(13,1),(1 4,2),(15,3),(16,4),(17,5), (18,6) And lagnum always has to be greater than or equal to 1 and less than or equal to (number of cols/number of rows ). Thanks For your advice. -Original Message- From: Bert Gunter [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 08, 2007 5:34 PM To: Leeds, Mark (IED); r-help@stat.math.ethz.ch Subject: RE: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function Suggestion: You might make it easier for folks to help if you explained in clear and simple terms what you are trying to do. Code is hard to deconstruct. Bert Gunter Genentech Nonclinical Statistics -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Leeds, Mark (IED) Sent: Tuesday, May 08, 2007 2:22 PM To: r-help@stat.math.ethz.ch Subject: [R] Looking for a cleaner way to implement a setting certainindices of a matrix to 1 function I wrote an ugly algorithm to set certain elements of a matrix to 1 without looping and below works and you can see what The output is below the code. K-6 lagnum-2 restrictmat-matrix(0,nrow=K,ncol=K*3) restrictmat[((col(restrictmat) - row(restrictmat) = 0 ) (col(restrictmat)-row(restrictmat)) %% K == 0)]-1 restrictmat[,(lagnum*K+1):ncol(restrictmat)]-0 restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000100 0 0 0 0 0 0 0 0 0 [2,]010000010 0 0 0 0 0 0 0 0 0 [3,]001000001 0 0 0 0 0 0 0 0 0 [4,]000100000 1 0 0 0 0 0 0 0 0 [5,]000010000 0 1 0 0 0 0 0 0 0 [6,]000001000 0 0 1 0 0 0 0 0 0 For lagnum equals 1 , it also works : restrictmat [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [1,]100000000 0 0 0 0 0 0 0 0 0 [2,]010000000 0 0 0 0 0 0 0 0 0 [3,]001000000 0 0 0 0 0 0 0 0 0 [4,]000100000 0 0 0 0 0 0 0 0 0 [5,]000010000 0 0 0 0 0 0 0 0 0 [6,]000001000 0 0 0 0 0 0 0 0 0 But I am thinking that there has to be a better way particularly because I'll get an error if I set lagnum to 3. Any improvements or total revampings are appreciated. The number of columns will always be a multiple of the number of rows So K doesn't have to be 6. that was just to show what the commands do. thanks. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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. This is not an offer (or solicitation of an offer) to buy/se...{{dropped}} __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.