MATLAB is missing one essential rounding function!

조회 수: 6 (최근 30일)
Jim Luschen
Jim Luschen 2011년 10월 26일
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.

채택된 답변

Daniel Shub
Daniel Shub 2011년 10월 26일
ceilfix = @(x)ceil(abs(x)).*sign(x);
or some such nonsense name.
ceilfix(-2:0.2:2)
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 10월 26일
I like your algorithm better!
Jim Luschen
Jim Luschen 2011년 10월 26일
you guys crack me up! thanks again for the help.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Sean de Wolski
Sean de Wolski 2011년 10월 26일
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.

Jim Luschen
Jim Luschen 2011년 10월 26일
Thank you Sean and Daniel -- I bow to your superior intellects!
But I still think MATLAB should add the function to their library. Similar trickery to yours can conjure "floor" from "ceil", and yet MATLAB includes both of those functions in their library.
  댓글 수: 3
Daniel Shub
Daniel Shub 2011년 10월 26일
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
Daniel Shub 2011년 10월 26일
stop beating me Sean.

댓글을 달려면 로그인하십시오.


Tom Smits
Tom Smits 2013년 3월 14일
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
Walter Roberson
Walter Roberson 2013년 3월 14일
To confirm: is that addition rather than multiplication ?
Teja Muppirala
Teja Muppirala 2013년 3월 15일
This won't work for nonzero integers:
ceilfix(1.0) = sign(1.0) + fix(1.0) = 1.0 + 1.0 = 2

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by