x<-matrix(c(1,0,0,0,1,0,0,0,1),nrow=3)> y<-matrix(c(0,0,0,1,0,0,1,1,0),nrow=3) > z<-matrix(c(0,1,0,0,1,0,1,0,0),nrow=3) > x[z]<-y[z]The resultant matrix x is all zeros except for the last two diagonal cells which are 1's. While y is lower triangualr 0's with the remaining cells all ones. I really don't understand how this deceptively simple looking piece of code is giving that result can someone explain please. I'm obviously missing something pretty basic so please keep your answer suitably basic. -- View this message in context: http://r.789695.n4.nabble.com/Confused-by-code-tp4643946.html Sent from the R help mailing list archive at Nabble.com.
Tena koe I think you probably meant: x[as.logical(z)] <- y[as.logical(z)] i.e., choosing those elements of ? and y where z is 1 (TRUE as logical). Whereas what you have written: ?[z] <- y[z] references the 0th (by default indexing starts at 1 so this is empty (see ?[0]) and the first element of ? and y (repeatedly). Hope this helps .... Peter Alspach -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Bazman76 Sent: Monday, 24 September 2012 8:53 a.m. To: r-help at r-project.org Subject: [R] Confused by code? x<-matrix(c(1,0,0,0,1,0,0,0,1),nrow=3)> y<-matrix(c(0,0,0,1,0,0,1,1,0),nrow=3) > z<-matrix(c(0,1,0,0,1,0,1,0,0),nrow=3) > x[z]<-y[z]The resultant matrix x is all zeros except for the last two diagonal cells which are 1's. While y is lower triangualr 0's with the remaining cells all ones. I really don't understand how this deceptively simple looking piece of code is giving that result can someone explain please. I'm obviously missing something pretty basic so please keep your answer suitably basic. -- View this message in context: http://r.789695.n4.nabble.com/Confused-by-code-tp4643946.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Hello, It is pretty basic, and it is deceptively simple. The worst of all :) When you index a matrix 'x' by another matrix 'z' the index can be a logical matrix of the same dimensions or recyclable to the dims of 'x', it can be a matrix with only two columns, a row numbers column and a column numbers one, or your case. In your case, 'z' is coerced to vector, and the values in 'z' are taken to be indexes to 'x'. But since you only have two distinct values and one of them is zero, it will only return x[1] three times (there are three 1s in 'z'). The same goes for 'y'. Correct: # Create an index matrix z.inx <- which(z == 1, arr.ind = TRUE) z.inx # Test x1 <- x2 <- x3 <- x # Use copies to test x1[z == 1] <- y[z == 1] x2[z.inx] <- y[z.inx] # 1 and 0 to T/F x3[as.logical(z)] <- y[as.logical(z)] x1 identical(x1, x2) identical(x1, x3) Hope this helps, Rui Barradas Em 23-09-2012 21:52, Bazman76 escreveu:> x<-matrix(c(1,0,0,0,1,0,0,0,1),nrow=3) >> y<-matrix(c(0,0,0,1,0,0,1,1,0),nrow=3) >> z<-matrix(c(0,1,0,0,1,0,1,0,0),nrow=3) >> x[z]<-y[z] > The resultant matrix x is all zeros except for the last two diagonal cells > which are 1's. > While y is lower triangualr 0's with the remaining cells all ones. > > I really don't understand how this deceptively simple looking piece of code > is giving that result can someone explain please. > I'm obviously missing something pretty basic so please keep your answer > suitably basic. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Confused-by-code-tp4643946.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Thanks Rui Barrudas and Peter Alspach, I understand better now: x<-matrix(c(1,0,0,0,2,0,0,0,2),nrow=3) y<-matrix(c(7,8,9,1,5,10,1,1,0),nrow=3) z<-matrix(c(0,1,0,0,0,0,6,0,0),nrow=3) x[z]<-y[z] viewData(x) produces an x matrix 7 0 0 0 2 0 0 10 2 which makes sense the first element of y 7 is inserted into z in slot x[1] and the and 6th element of y 10 is slotted into the x[6]. However the original code runs like this: mI<- mRU(de.d, de.nP)>de.CR mPV[mI]<mP[mI] where mPv and MP are both (de.d, de.nP) matrices. and mRU<-function(m,n){ return(array(runif(m*n), dim=c(m,n))) } i.e. it returns an array of m*n random numbers uniformly distributed between 0 and 1. de.CR is a fixed value say 0.8. So mI<- mRU(de.d, de.NP)>de.CR returns a de.d*de.nP array where each element is 1 is its more than 0.8 and zero otherwise. So in this case element mPv[1] will be repeatedly filled with the value of mP[1] and all other elements will remain unaffected? Is this correct? If so I am still confused as this is not what I thought was supposed to by happening but I know that the code overall does its job correctly? -- View this message in context: http://r.789695.n4.nabble.com/Confused-by-code-tp4643946p4644010.html Sent from the R help mailing list archive at Nabble.com.