Skip to content

extrat non diagonal

5 messages · malika yassa, S Ellison, Richard M. Heiberger +1 more

#
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
#
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
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)
*******************************************************************
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 postmaster at 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
#
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
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[,1] [,2] [,3]
[1,]    2    4    7
[2,]    3    6    8
On Wed, Nov 14, 2018 at 11:04 AM S Ellison <S.Ellison at lgcgroup.com> 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
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
[,1] [,2] [,3]
[1,]    2    4    7
[2,]    3    6    8
On Wed, Nov 14, 2018 at 2:09 PM Richard M. Heiberger <rmh at temple.edu> wrote:
#
Another way:
dimnames=list(Row=paste0("r",1:3),Col=paste0("c",1:3)))
Col
Row  c1 c2 c3
  r1  1  4  7
  r2  2  5  8
  r3  3  6  9
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 <rmh at temple.edu>
wrote: