Hi all, Assume I have a matrix xv= [1 0 0 0 0 12, 0 1 0 0 0 10, * 0 0 1 0 0 -9,* 0 0 0 1 0 20, * 0 0 0 0 1 -5]* if the last column of "xv" less than 0 then I want to set zero the entire row. The desired output looks like the following. In this case row 3 and row 5 are set zero. [ 1 0 0 0 0 12, 0 1 0 0 0 10, * 0 0 0 0 0 0,* 0 0 0 1 0 20, * 0 0 0 0 0 0*] I used ifelse command but did not work Is there another command to do that? Thanks in advance \ [[alternative HTML version deleted]]
Try this: replace(m, m[,ncol(m)] < 0, 0) On Tue, Apr 26, 2011 at 6:28 PM, Val <valkremk at gmail.com> wrote:> Hi all, > > Assume I have a matrix > xv= ?[1 0 0 0 0 12, > ? ? ?0 1 0 0 0 10, > ?* ? ?0 0 1 0 0 -9,* > ? ? ?0 0 0 1 0 20, > ? ?* ?0 0 0 0 1 -5]* > > if the last column of "xv" ?less than ?0 then I want to set zero the entire > row. > The desired output looks like the following. In this case row 3 and row 5 > are set zero. > > ? ?[ 1 0 0 0 0 12, > ? ? ?0 1 0 0 0 10, > ? ? * 0 0 0 0 0 0,* > ? ? ?0 0 0 1 0 20, > ? ?* ?0 0 0 0 0 0*] > > I used ifelse command but did not work > > Is there another command to do that? > > Thanks in advance > \ > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Tue, Apr 26, 2011 at 2:28 PM, Val <valkremk at gmail.com> wrote:> Hi all, > > Assume I have a matrix > xv= ?[1 0 0 0 0 12, > ? ? ?0 1 0 0 0 10, > ?* ? ?0 0 1 0 0 -9,* > ? ? ?0 0 0 1 0 20, > ? ?* ?0 0 0 0 1 -5]* > > if the last column of "xv" ?less than ?0 then I want to set zero the entire > row. > The desired output looks like the following. In this case row 3 and row 5 > are set zero. > > ? ?[ 1 0 0 0 0 12, > ? ? ?0 1 0 0 0 10, > ? ? * 0 0 0 0 0 0,* > ? ? ?0 0 0 1 0 20, > ? ?* ?0 0 0 0 0 0*] > > I used ifelse command but did not work > > Is there another command to do that?Yes. nc = ncol(xv) result = xv; result[ xv[, nc] <0, ] = 0 HTH, Peter