Hello all, I am working with a 1000x1000 matrix, and I would like to return a 1000x1000 matrix that tells me which value in the matrix is greater than a theshold value (1 or 0 indicator). i have tried mat2<-as.matrix(as.numeric(mat1>0.25)) but that returns a 1:100000 matrix. I have also tried for loops, but they are grossly inefficient. THanks for all your help in advance. Lanre
Is this what you want:> x <- matrix(runif(100), 10) > round(x, 3)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0.268 0.961 0.262 0.347 0.306 0.762 0.524 0.062 0.028 0.226 [2,] 0.219 0.100 0.165 0.131 0.578 0.933 0.317 0.109 0.527 0.131 [3,] 0.517 0.763 0.322 0.374 0.910 0.471 0.278 0.382 0.880 0.982 [4,] 0.269 0.948 0.510 0.631 0.143 0.604 0.788 0.169 0.373 0.327 [5,] 0.181 0.819 0.924 0.390 0.415 0.485 0.702 0.299 0.048 0.507 [6,] 0.519 0.308 0.511 0.690 0.211 0.109 0.165 0.192 0.139 0.681 [7,] 0.563 0.650 0.258 0.689 0.429 0.248 0.064 0.257 0.321 0.099 [8,] 0.129 0.953 0.046 0.555 0.133 0.499 0.755 0.181 0.155 0.119 [9,] 0.256 0.954 0.418 0.430 0.460 0.373 0.620 0.477 0.132 0.050 [10,] 0.718 0.340 0.854 0.453 0.943 0.935 0.170 0.771 0.221 0.929> ifelse(x > .5, 1, 0)[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 0 1 0 0 0 1 1 0 0 0 [2,] 0 0 0 0 1 1 0 0 1 0 [3,] 1 1 0 0 1 0 0 0 1 1 [4,] 0 1 1 1 0 1 1 0 0 0 [5,] 0 1 1 0 0 0 1 0 0 1 [6,] 1 0 1 1 0 0 0 0 0 1 [7,] 1 1 0 1 0 0 0 0 0 0 [8,] 0 1 0 1 0 0 1 0 0 0 [9,] 0 1 0 0 0 0 1 0 0 0 [10,] 1 0 1 0 1 1 0 1 0 1 On 8/10/07, Lanre Okusanya <lanre.okusanya at gmail.com> wrote:> Hello all, > > I am working with a 1000x1000 matrix, and I would like to return a > 1000x1000 matrix that tells me which value in the matrix is greater > than a theshold value (1 or 0 indicator). > i have tried > mat2<-as.matrix(as.numeric(mat1>0.25)) > but that returns a 1:100000 matrix. > I have also tried for loops, but they are grossly inefficient. > > THanks for all your help in advance. > > Lanre > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
that was ridiculously simple. duh. THanks Lanre On 8/10/07, jim holtman <jholtman at gmail.com> wrote:> Is this what you want: > > > x <- matrix(runif(100), 10) > > round(x, 3) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 0.268 0.961 0.262 0.347 0.306 0.762 0.524 0.062 0.028 0.226 > [2,] 0.219 0.100 0.165 0.131 0.578 0.933 0.317 0.109 0.527 0.131 > [3,] 0.517 0.763 0.322 0.374 0.910 0.471 0.278 0.382 0.880 0.982 > [4,] 0.269 0.948 0.510 0.631 0.143 0.604 0.788 0.169 0.373 0.327 > [5,] 0.181 0.819 0.924 0.390 0.415 0.485 0.702 0.299 0.048 0.507 > [6,] 0.519 0.308 0.511 0.690 0.211 0.109 0.165 0.192 0.139 0.681 > [7,] 0.563 0.650 0.258 0.689 0.429 0.248 0.064 0.257 0.321 0.099 > [8,] 0.129 0.953 0.046 0.555 0.133 0.499 0.755 0.181 0.155 0.119 > [9,] 0.256 0.954 0.418 0.430 0.460 0.373 0.620 0.477 0.132 0.050 > [10,] 0.718 0.340 0.854 0.453 0.943 0.935 0.170 0.771 0.221 0.929 > > ifelse(x > .5, 1, 0) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 0 1 0 0 0 1 1 0 0 0 > [2,] 0 0 0 0 1 1 0 0 1 0 > [3,] 1 1 0 0 1 0 0 0 1 1 > [4,] 0 1 1 1 0 1 1 0 0 0 > [5,] 0 1 1 0 0 0 1 0 0 1 > [6,] 1 0 1 1 0 0 0 0 0 1 > [7,] 1 1 0 1 0 0 0 0 0 0 > [8,] 0 1 0 1 0 0 1 0 0 0 > [9,] 0 1 0 0 0 0 1 0 0 0 > [10,] 1 0 1 0 1 1 0 1 0 1 > > > On 8/10/07, Lanre Okusanya <lanre.okusanya at gmail.com> wrote: > > Hello all, > > > > I am working with a 1000x1000 matrix, and I would like to return a > > 1000x1000 matrix that tells me which value in the matrix is greater > > than a theshold value (1 or 0 indicator). > > i have tried > > mat2<-as.matrix(as.numeric(mat1>0.25)) > > but that returns a 1:100000 matrix. > > I have also tried for loops, but they are grossly inefficient. > > > > THanks for all your help in advance. > > > > Lanre > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? >
mat2<-matrix(as.numeric(mat1>0.25), ncol=1000) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O On 10/08/07, Lanre Okusanya <lanre.okusanya@gmail.com> wrote:> > Hello all, > > I am working with a 1000x1000 matrix, and I would like to return a > 1000x1000 matrix that tells me which value in the matrix is greater > than a theshold value (1 or 0 indicator). > i have tried > mat2<-as.matrix(as.numeric(mat1>0.25)) > but that returns a 1:100000 matrix. > I have also tried for loops, but they are grossly inefficient. > > THanks for all your help in advance. > > Lanre > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
An even simpler solution is: mat2 <- 1 * (mat1 > 0.25) Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Lanre Okusanya Sent: Friday, August 10, 2007 2:20 PM To: jim holtman Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Help wit matrices that was ridiculously simple. duh. THanks Lanre On 8/10/07, jim holtman <jholtman at gmail.com> wrote:> Is this what you want: > > > x <- matrix(runif(100), 10) > > round(x, 3) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 0.268 0.961 0.262 0.347 0.306 0.762 0.524 0.062 0.028 0.226 > [2,] 0.219 0.100 0.165 0.131 0.578 0.933 0.317 0.109 0.527 0.131 > [3,] 0.517 0.763 0.322 0.374 0.910 0.471 0.278 0.382 0.880 0.982 > [4,] 0.269 0.948 0.510 0.631 0.143 0.604 0.788 0.169 0.373 0.327 > [5,] 0.181 0.819 0.924 0.390 0.415 0.485 0.702 0.299 0.048 0.507 > [6,] 0.519 0.308 0.511 0.690 0.211 0.109 0.165 0.192 0.139 0.681 > [7,] 0.563 0.650 0.258 0.689 0.429 0.248 0.064 0.257 0.321 0.099 > [8,] 0.129 0.953 0.046 0.555 0.133 0.499 0.755 0.181 0.155 0.119 > [9,] 0.256 0.954 0.418 0.430 0.460 0.373 0.620 0.477 0.132 0.050 > [10,] 0.718 0.340 0.854 0.453 0.943 0.935 0.170 0.771 0.221 0.929 > > ifelse(x > .5, 1, 0) > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 0 1 0 0 0 1 1 0 0 0 > [2,] 0 0 0 0 1 1 0 0 1 0 > [3,] 1 1 0 0 1 0 0 0 1 1 > [4,] 0 1 1 1 0 1 1 0 0 0 > [5,] 0 1 1 0 0 0 1 0 0 1 > [6,] 1 0 1 1 0 0 0 0 0 1 > [7,] 1 1 0 1 0 0 0 0 0 0 > [8,] 0 1 0 1 0 0 1 0 0 0 > [9,] 0 1 0 0 0 0 1 0 0 0 > [10,] 1 0 1 0 1 1 0 1 0 1 > > > On 8/10/07, Lanre Okusanya <lanre.okusanya at gmail.com> wrote: > > Hello all, > > > > I am working with a 1000x1000 matrix, and I would like to return a > > 1000x1000 matrix that tells me which value in the matrix is greater > > than a theshold value (1 or 0 indicator). > > i have tried > > mat2<-as.matrix(as.numeric(mat1>0.25)) > > but that returns a 1:100000 matrix. > > I have also tried for loops, but they are grossly inefficient. > > > > THanks for all your help in advance. > > > > Lanre > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? >______________________________________________ R-help at stat.math.ethz.ch 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.
Will something like this help? mm <- matrix(rnorm(100),nrow=10) mm nn <- mm >.5 nn --- Lanre Okusanya <lanre.okusanya at gmail.com> wrote:> Hello all, > > I am working with a 1000x1000 matrix, and I would > like to return a > 1000x1000 matrix that tells me which value in the > matrix is greater > than a theshold value (1 or 0 indicator). > i have tried > mat2<-as.matrix(as.numeric(mat1>0.25)) > but that returns a 1:100000 matrix. > I have also tried for loops, but they are grossly > inefficient. > > THanks for all your help in advance. > > Lanre > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
On 10-Aug-07 18:05:50, Lanre Okusanya wrote:> Hello all, > > I am working with a 1000x1000 matrix, and I would like to return a > 1000x1000 matrix that tells me which value in the matrix is greater > than a theshold value (1 or 0 indicator). > i have tried > mat2<-as.matrix(as.numeric(mat1>0.25)) > but that returns a 1:100000 matrix. > I have also tried for loops, but they are grossly inefficient. > > THanks for all your help in advance. > > LanreSimple-minded, but:> S<-matrix(rnorm(25),nrow=5) > S[,1] [,2] [,3] [,4] [,5] [1,] -0.9283624 -0.44418487 1.1174555 1.9040999 -0.4675796 [2,] 0.2658770 -0.28492642 -1.2271013 -0.5713291 1.8036235 [3,] 0.7010885 -0.42972262 0.7576021 0.3407972 -1.0628487 [4,] -0.2003087 0.87006841 0.6233792 -0.9974902 -0.9104270 [5,] 0.2729014 0.09781886 -1.0004486 1.5987385 -0.4747125> T<-0*S > T[S>0.25] <- 1+0*S[S>0.25] > T[,1] [,2] [,3] [,4] [,5] [1,] 0 0 1 1 0 [2,] 1 0 0 0 1 [3,] 1 0 1 1 0 [4,] 0 1 1 0 0 [5,] 1 0 0 1 0 Does this work OK for your big matrix? HTH Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 10-Aug-07 Time: 19:50:37 ------------------------------ XFMail ------------------------------
I hope you don't mind that I offer also two solutions. No.1 is really bad. No.2 should be on par with the other ones. Best, Roland mydata <- matrix(rnorm(10*10), ncol=10) threshold.value <- 1.5 mydata2 <- matrix(0, nrow=nrow(mydata), ncol=ncol(mydata)) mydata3 <- matrix(0, nrow=nrow(mydata), ncol=ncol(mydata)) ### not really the way to go: for (i in 1:nrow(mydata)) { for (j in 1:ncol(mydata)) { if (mydata[i,j]>threshold.value) { mydata2[i,j] <- 1 } } } ### a better way... mydata3[mydata > threshold.value] <- 1 mydata2 mydata3 Lanre Okusanya wrote:> Hello all, > > I am working with a 1000x1000 matrix, and I would like to return a > 1000x1000 matrix that tells me which value in the matrix is greater > than a theshold value (1 or 0 indicator). > i have tried > mat2<-as.matrix(as.numeric(mat1>0.25)) > but that returns a 1:100000 matrix. > I have also tried for loops, but they are grossly inefficient. > > THanks for all your help in advance. > > Lanre > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >