Re: [R] R: Why this deosn't work?, matrix, rounding error?
On Fri, 2010-10-08 at 09:30 -0700, skan wrote: It's a problem much bigger. I use a matrix to store the results of a bigger problem. I loop through several variables and store the results of a computation on that matrix. At the beginning of the problem I initialize the matrix to zeros and I calculate its size from some input. And that seems not to work well maybe because of some rounding error. Several people have responded with a solution to your Q on stackoverflow: matrix(0, ncota*nslope, 4) As the 0 will get recycled to appropriate length. G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.
Re: [R] R: Why this deosn't work?, matrix, rounding error?
Hello I've seen the answer at stackoverflow. They also said I must use zapsmall to avoid roundup problems. I didn't expect this behaviour when division gives an integer number. -- View this message in context: http://r.789695.n4.nabble.com/R-Why-this-deosn-t-work-matrix-rounding-error-tp2968527p2969459.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] R: Why this deosn't work?, matrix, rounding error?
On 2010-10-09 4:47, skan wrote: Hello I've seen the answer at stackoverflow. They also said I must use zapsmall to avoid roundup problems. I didn't expect this behaviour when division gives an integer number. The trouble is that your expectations may not coincide with reality. That's why people refer you to FAQ 7.31. Even replacing the rep(0, ) with just 0 will not necessarily give the expected result: eps1 - 1e-16 eps2 - 1e-15 ## try to generate a 3-by-4 matrix: matrix(0, nrow = 3 - eps1, ncol = 4) # [,1] [,2] [,3] [,4] #[1,]0000 #[2,]0000 #[3,]0000 matrix(0, nrow = 3 - eps2, ncol = 4) # [,1] [,2] [,3] [,4] #[1,]0000 #[2,]0000 matrix(0, nrow = zapsmall(3 - eps2), ncol = 4) # [,1] [,2] [,3] [,4] #[1,]0000 #[2,]0000 #[3,]0000 ## Note that your calculation did _not_ yield an integer: 1 + ((1.5 - 0.1) / 0.05) - 29 #[1] -3.552714e-15 Such are the vagaries of floating-point arithmetic. Play it safe; use zapsmall. -Peter Ehlers __ R-help@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.
[R] R: Why this deosn't work?, matrix, rounding error?
Hello Why this works: ncota - 1 nslope - 29 resul - matrix(rep(0,ncota*nslope*4),ncota*nslope,4) But this doesn't? ncota - 1 sini - 0.1; sfin - 1.5; spaso - 0.05; nslope - 1+((sfin-sini)/spaso) resul - matrix(rep(0,ncota*nslope*4),ncota*nslope,4) I guess the problem is that the division gives a noninteger number. How can I get the second one work? I need to create a zero matrix with its size calculated from a calculation. cheers -- View this message in context: http://r.789695.n4.nabble.com/R-Why-this-deosn-t-work-matrix-rounding-error-tp2968527p2968527.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] R: Why this deosn't work?, matrix, rounding error?
FAQ 7.31 On Fri, Oct 8, 2010 at 11:27 AM, skan juanp...@gmail.com wrote: Hello Why this works: ncota - 1 nslope - 29 resul - matrix(rep(0,ncota*nslope*4),ncota*nslope,4) But this doesn't? ncota - 1 sini - 0.1; sfin - 1.5; spaso - 0.05; nslope - 1+((sfin-sini)/spaso) resul - matrix(rep(0,ncota*nslope*4),ncota*nslope,4) I guess the problem is that the division gives a noninteger number. How can I get the second one work? I need to create a zero matrix with its size calculated from a calculation. cheers -- View this message in context: http://r.789695.n4.nabble.com/R-Why-this-deosn-t-work-matrix-rounding-error-tp2968527p2968527.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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.
Re: [R] R: Why this deosn't work?, matrix, rounding error?
To make it work, use round to take care of the issues in FAQ 7.31 resul - matrix(rep(0,round(ncota*nslope*4)),round(ncota*nslope),4) On Fri, Oct 8, 2010 at 11:27 AM, skan juanp...@gmail.com wrote: Hello Why this works: ncota - 1 nslope - 29 resul - matrix(rep(0,ncota*nslope*4),ncota*nslope,4) But this doesn't? ncota - 1 sini - 0.1; sfin - 1.5; spaso - 0.05; nslope - 1+((sfin-sini)/spaso) resul - matrix(rep(0,ncota*nslope*4),ncota*nslope,4) I guess the problem is that the division gives a noninteger number. How can I get the second one work? I need to create a zero matrix with its size calculated from a calculation. cheers -- View this message in context: http://r.789695.n4.nabble.com/R-Why-this-deosn-t-work-matrix-rounding-error-tp2968527p2968527.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ R-help@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.
Re: [R] R: Why this deosn't work?, matrix, rounding error?
It's a problem much bigger. I use a matrix to store the results of a bigger problem. I loop through several variables and store the results of a computation on that matrix. At the beginning of the problem I initialize the matrix to zeros and I calculate its size from some input. And that seems not to work well maybe because of some rounding error. -- View this message in context: http://r.789695.n4.nabble.com/R-Why-this-deosn-t-work-matrix-rounding-error-tp2968527p2968662.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.