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
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
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,]
> 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 attach
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
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]
[
> -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<-
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-p
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.
Mi
> 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)],
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]]
__
11 matches
Mail list logo