Re: [R] extrat non diagonal
Another way: > A <- matrix(1:9,3,3, dimnames=list(Row=paste0("r",1:3),Col=paste0("c",1:3))) > A Col Row c1 c2 c3 r1 1 4 7 r2 2 5 8 r3 3 6 9 > matrix( A[row(A)!=col(A)], nrow(A)-1, ncol(A), dimnames=list(NULL, colnames(A))) c1 c2 c3 [1,] 2 4 7 [2,] 3 6 8 Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Nov 14, 2018 at 2:32 PM, Richard M. Heiberger wrote: > An even better solution because it has fewer steps. > > A <- matrix(1:9, 3, 3) > A > B <- A[-1, ] > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > B > > > A <- matrix(1:9, 3, 3) > > A > [,1] [,2] [,3] > [1,]147 > [2,]258 > [3,]369 > > B <- A[-1, ] > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > B > [,1] [,2] [,3] > [1,]247 > [2,]368 > > > On Wed, Nov 14, 2018 at 2:09 PM Richard M. Heiberger > wrote: > > > > Steve's method is very slick. > > > > I think this is a bit easier to understand. > > > > A <- matrix(1:9, 3, 3) > > A > > B <- matrix(nrow=2, ncol=3) > > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > B > > > > > A <- matrix(1:9, 3, 3) > > > A > > [,1] [,2] [,3] > > [1,]147 > > [2,]258 > > [3,]369 > > > B <- matrix(nrow=2, ncol=3) > > > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > > B > > [,1] [,2] [,3] > > [1,]247 > > [2,]368 > > > > > On Wed, Nov 14, 2018 at 11:04 AM S Ellison > wrote: > > > > > > i) Your code creates w2 but references w1 to create aa. > > > > > > So you needed > > > aa <- matrix(rep(c(0.4, 0.1, 0.2), 3), 3,3) > > > for a working example. > > > > > > ii) This > > > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > > removes any value that is present in the diagonal of aa. Look up > ?"%in%" to see what that does; it returns TRUE whenever anything in > as.numeric(aa) matches anything in your diagonal. All the values in aa > match one of c(0.4, 0.1, 0.2). So since your whole matrix consists of these > three numbers, you told R to leave out everything in aa and then create a > 2x3 matrix with the result. Hence the NAs > > > > > > iii) If you want to extract odd parts of a matrix explicitly, see ?"[" > and particularly the section on indexing using arrays > > > > > > iv) You can use logical indexing. In the special case of the diagonal, > you can use diag() to create a matrix of logicals, logically negate that > and apply that to your matrix: > > > aa[ !diag(rep(TRUE, 3)) ] > > > > > > and, in twoi rows: > > > matrix( aa[ !diag(rep(TRUE, 3)) ], 2,3) > > > > > > > for examplei have this matrix > > > > w2<-c(0.1,0.2,0.4,0.2,0.4,0.1) > > > > aa<-matrix(w1,nrow=3,ncol=3) > > > > aa > > > > [,1] [,2] [,3] > > > > [1,] 0.4 0.4 0.4 > > > > [2,] 0.1 0.1 0.1 > > > > [3,] 0.2 0.2 0.2 > > > > > > > > if i use this code > > > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > > > > > > > i will obtaine this matrix[,1] [,2] [,3] > > > > [1,] NA NA NA > > > > [2,] NA NA NA > > > > > > > > but me i want this matrix[,1] [,2] [,3] > > > > [1,] 0.1 0.4 0.4 > > > > [2,] 0.2 0.2 0.1 > > > > > > > > thank you > > > > > > > > [[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. > > > > > > > > > *** > > > This email and any attachments are confidential. Any use, copying or > > > disclosure other than by the intended recipient is unauthorised. If > > > you have received this message in error, please notify the sender > > > immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com > > > and delete this message and any copies from your computer and network. > > > LGC Limited. Registered in England 2991879. > > > Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > > > __ > > > 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. > [[alternative HTML version deleted]]
Re: [R] extrat non diagonal
An even better solution because it has fewer steps. A <- matrix(1:9, 3, 3) A B <- A[-1, ] B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] B > A <- matrix(1:9, 3, 3) > A [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 > B <- A[-1, ] > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > B [,1] [,2] [,3] [1,]247 [2,]368 > On Wed, Nov 14, 2018 at 2:09 PM Richard M. Heiberger wrote: > > Steve's method is very slick. > > I think this is a bit easier to understand. > > A <- matrix(1:9, 3, 3) > A > B <- matrix(nrow=2, ncol=3) > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > B > > > A <- matrix(1:9, 3, 3) > > A > [,1] [,2] [,3] > [1,]147 > [2,]258 > [3,]369 > > B <- matrix(nrow=2, ncol=3) > > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > B > [,1] [,2] [,3] > [1,]247 > [2,]368 > > > On Wed, Nov 14, 2018 at 11:04 AM S Ellison wrote: > > > > i) Your code creates w2 but references w1 to create aa. > > > > So you needed > > aa <- matrix(rep(c(0.4, 0.1, 0.2), 3), 3,3) > > for a working example. > > > > ii) This > > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > removes any value that is present in the diagonal of aa. Look up ?"%in%" to > > see what that does; it returns TRUE whenever anything in as.numeric(aa) > > matches anything in your diagonal. All the values in aa match one of c(0.4, > > 0.1, 0.2). So since your whole matrix consists of these three numbers, you > > told R to leave out everything in aa and then create a 2x3 matrix with the > > result. Hence the NAs > > > > iii) If you want to extract odd parts of a matrix explicitly, see ?"[" and > > particularly the section on indexing using arrays > > > > iv) You can use logical indexing. In the special case of the diagonal, you > > can use diag() to create a matrix of logicals, logically negate that and > > apply that to your matrix: > > aa[ !diag(rep(TRUE, 3)) ] > > > > and, in twoi rows: > > matrix( aa[ !diag(rep(TRUE, 3)) ], 2,3) > > > > > for examplei have this matrix > > > w2<-c(0.1,0.2,0.4,0.2,0.4,0.1) > > > aa<-matrix(w1,nrow=3,ncol=3) > > > aa > > > [,1] [,2] [,3] > > > [1,] 0.4 0.4 0.4 > > > [2,] 0.1 0.1 0.1 > > > [3,] 0.2 0.2 0.2 > > > > > > if i use this code > > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > > > > > i will obtaine this matrix[,1] [,2] [,3] > > > [1,] NA NA NA > > > [2,] NA NA NA > > > > > > but me i want this matrix[,1] [,2] [,3] > > > [1,] 0.1 0.4 0.4 > > > [2,] 0.2 0.2 0.1 > > > > > > thank you > > > > > > [[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. > > > > > > *** > > This email and any attachments are confidential. Any use, copying or > > disclosure other than by the intended recipient is unauthorised. If > > you have received this message in error, please notify the sender > > immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com > > and delete this message and any copies from your computer and network. > > LGC Limited. Registered in England 2991879. > > Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > > __ > > 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.
Re: [R] extrat non diagonal
Steve's method is very slick. I think this is a bit easier to understand. A <- matrix(1:9, 3, 3) A B <- matrix(nrow=2, ncol=3) B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] B > A <- matrix(1:9, 3, 3) > A [,1] [,2] [,3] [1,]147 [2,]258 [3,]369 > B <- matrix(nrow=2, ncol=3) > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > B [,1] [,2] [,3] [1,]247 [2,]368 > On Wed, Nov 14, 2018 at 11:04 AM S Ellison wrote: > > i) Your code creates w2 but references w1 to create aa. > > So you needed > aa <- matrix(rep(c(0.4, 0.1, 0.2), 3), 3,3) > for a working example. > > ii) This > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > removes any value that is present in the diagonal of aa. Look up ?"%in%" to > see what that does; it returns TRUE whenever anything in as.numeric(aa) > matches anything in your diagonal. All the values in aa match one of c(0.4, > 0.1, 0.2). So since your whole matrix consists of these three numbers, you > told R to leave out everything in aa and then create a 2x3 matrix with the > result. Hence the NAs > > iii) If you want to extract odd parts of a matrix explicitly, see ?"[" and > particularly the section on indexing using arrays > > iv) You can use logical indexing. In the special case of the diagonal, you > can use diag() to create a matrix of logicals, logically negate that and > apply that to your matrix: > aa[ !diag(rep(TRUE, 3)) ] > > and, in twoi rows: > matrix( aa[ !diag(rep(TRUE, 3)) ], 2,3) > > > for examplei have this matrix > > w2<-c(0.1,0.2,0.4,0.2,0.4,0.1) > > aa<-matrix(w1,nrow=3,ncol=3) > > aa > > [,1] [,2] [,3] > > [1,] 0.4 0.4 0.4 > > [2,] 0.1 0.1 0.1 > > [3,] 0.2 0.2 0.2 > > > > if i use this code > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > > > i will obtaine this matrix[,1] [,2] [,3] > > [1,] NA NA NA > > [2,] NA NA NA > > > > but me i want this matrix[,1] [,2] [,3] > > [1,] 0.1 0.4 0.4 > > [2,] 0.2 0.2 0.1 > > > > thank you > > > > [[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. > > > *** > This email and any attachments are confidential. Any u...{{dropped:14}} __ 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.
Re: [R] extrat non diagonal
> With your code you just remove diagonal elements from your matrix. Worse; it removed _all_ elements from the matrix that match _anything_ in the diagonal! Which, in that example, was everything ... *** This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK __ 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.
Re: [R] extrat non diagonal
i) Your code creates w2 but references w1 to create aa. So you needed aa <- matrix(rep(c(0.4, 0.1, 0.2), 3), 3,3) for a working example. ii) This > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) removes any value that is present in the diagonal of aa. Look up ?"%in%" to see what that does; it returns TRUE whenever anything in as.numeric(aa) matches anything in your diagonal. All the values in aa match one of c(0.4, 0.1, 0.2). So since your whole matrix consists of these three numbers, you told R to leave out everything in aa and then create a 2x3 matrix with the result. Hence the NAs iii) If you want to extract odd parts of a matrix explicitly, see ?"[" and particularly the section on indexing using arrays iv) You can use logical indexing. In the special case of the diagonal, you can use diag() to create a matrix of logicals, logically negate that and apply that to your matrix: aa[ !diag(rep(TRUE, 3)) ] and, in twoi rows: matrix( aa[ !diag(rep(TRUE, 3)) ], 2,3) > for examplei have this matrix > w2<-c(0.1,0.2,0.4,0.2,0.4,0.1) > aa<-matrix(w1,nrow=3,ncol=3) > aa > [,1] [,2] [,3] > [1,] 0.4 0.4 0.4 > [2,] 0.1 0.1 0.1 > [3,] 0.2 0.2 0.2 > > if i use this code > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > i will obtaine this matrix[,1] [,2] [,3] > [1,] NA NA NA > [2,] NA NA NA > > but me i want this matrix[,1] [,2] [,3] > [1,] 0.1 0.4 0.4 > [2,] 0.2 0.2 0.1 > > thank you > > [[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. *** This email and any attachments are confidential. Any use, copying or disclosure other than by the intended recipient is unauthorised. If you have received this message in error, please notify the sender immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com and delete this message and any copies from your computer and network. LGC Limited. Registered in England 2991879. Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK __ 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] extrat non diagonal
hello for examplei have this matrix w2<-c(0.1,0.2,0.4,0.2,0.4,0.1)aa<-matrix(w1,nrow=3,ncol=3)aa [,1] [,2] [,3] [1,] 0.4 0.4 0.4 [2,] 0.1 0.1 0.1 [3,] 0.2 0.2 0.2 if i use this code matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) i will obtaine this matrix[,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA but me i want this matrix[,1] [,2] [,3] [1,] 0.1 0.4 0.4 [2,] 0.2 0.2 0.1 thank you [[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.
Re: [R] extrat non diagonal
Hi Your mail is mess due to HTML formating. Please use plain taxt mail. You got an advice, did you try it? With your code you just remove diagonal elements from your matrix. If this is not your intention, you should specify more clearly what do you want to achieve as the result. Cheers Petr > -Original Message- > From: R-help On Behalf Of malika yassa via R- > help > Sent: Wednesday, November 14, 2018 1:09 PM > To: R-help Mailing List > Subject: [R] extrat non diagonal > > helloi didn't obtaine the matrix after extrat non diagonalmy programx<- > rnorm(6,0,1) > > aa<-matrix(x,nrow=6,ncol=6) > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],5,6) > > nrow=5ncol=6thank you > > [[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. Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/ Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/ __ 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] extrat non diagonal
helloi didn't obtaine the matrix after extrat non diagonalmy programx<-rnorm(6,0,1) aa<-matrix(x,nrow=6,ncol=6) matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],5,6) nrow=5ncol=6thank you [[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.
Re: [R] extrat non diagonal value
When that arrived it was a complete mess since you posted in HTML which scrambles your code and you sent code which had syntax errors. Please try again by posting in plain text and cut and paste your code. It would also help if you stated exactly what you expected your output to consist of. Michael On 14/11/2018 11:19, malika yassa via R-help wrote: helloplease i have this matrixx<-rnorm(6,0,1) aa<-matrix(x,nrow=6,ncol=6) i have to extrat non diagonal value, i use this code matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],5,6) but i didn't get the resultthank you [[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. -- Michael http://www.dewey.myzen.co.uk/home.html __ 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.
Re: [R] extrat non diagonal value
Hi. You did not specify what do you want to do with the result. functions upper.tri, lower.tri and diag can manipulate parts of matrices. Cheers Petr > -Original Message- > From: R-help On Behalf Of malika yassa via R- > help > Sent: Wednesday, November 14, 2018 12:20 PM > To: R-help Mailing List > Subject: [R] extrat non diagonal value > > helloplease i have this matrixx<-rnorm(6,0,1) > > aa<-matrix(x,nrow=6,ncol=6) > > i have to extrat non diagonal value, i use this code > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],5,6) > > but i didn't get the resultthank you > > [[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. Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/ Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/ __ 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] extrat non diagonal value
helloplease i have this matrixx<-rnorm(6,0,1) aa<-matrix(x,nrow=6,ncol=6) i have to extrat non diagonal value, i use this code matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],5,6) but i didn't get the resultthank you [[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.