MATLAB is missing one essential rounding function!
Mostrar comentarios más antiguos
Okay, besides the normal rounding function we all know and love ("round"; rounding towards the nearest integer), we have the three special rounding functions: ceil, floor, and fix. We can use them to round towards positive infinity, negative infinity, and towards zero, respectively. But there is no function for rounding AWAY FROM ZERO.
Come on, guys. Let's complete the group of functions and get that last one defined and implemented. I need it right now.
Respuesta aceptada
Más respuestas (3)
Sean de Wolski
el 26 de Oct. de 2011
function out = rafz(in)
%out = rafz(in), round away from zero
%SCd 10/26/2011
%
out = max(abs(ceil(in)),abs(floor(in))).*sign(in);
end
Now you have it! I couldn't come up with a better title, unfortunately.
1 comentario
Daniel Shub
el 26 de Oct. de 2011
I like your name better than mine.
Jim Luschen
el 26 de Oct. de 2011
0 votos
3 comentarios
Sean de Wolski
el 26 de Oct. de 2011
submit an enhancement request.
Daniel Shub
el 26 de Oct. de 2011
But who is going to get their answer accepted? Sean was first and had a good name. I was second and had a crappy name, but potentially a better algorithm.
Make a request: http://www.mathworks.com/support/service_request/login_servicerequests.html?targetURL=/support/service_requests/contact_support.do
Daniel Shub
el 26 de Oct. de 2011
stop beating me Sean.
Tom Smits
el 14 de Mzo. de 2013
0 votos
I was surprised as well that there was no 'round away from zero' function in MATLAB but eventually I figured that the solution is really simple, so I thought I'd share my findings:
ceilfix(x) = sign(x)+fix(x)
cheers
2 comentarios
Walter Roberson
el 14 de Mzo. de 2013
To confirm: is that addition rather than multiplication ?
Teja Muppirala
el 15 de Mzo. de 2013
This won't work for nonzero integers:
ceilfix(1.0) = sign(1.0) + fix(1.0) = 1.0 + 1.0 = 2
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!