필터 지우기
필터 지우기

rounding elements in array to nearest 0.25

조회 수: 130 (최근 30일)
Srinivas
Srinivas 2011년 8월 25일
댓글: Matthias Schneider 2023년 12월 4일
i need to round off some numbers to nearest quarter , with an option to round up or round down to nearest quarter
for e.g. For an array for (5,1) with following values
1373.698
1385.024
1394.82
1400.436
1396.532
output for rounding up to closest 0.25 will result in
1373.75
1385.25
1395.00
1400.50
1396.75
output for rounding down to closest 0.25 will be
1373.50
1385.00
1394.75
1400.25
1396.50
I am not sure how to create such a function which gives this output, please help .
  댓글 수: 1
Tod Wood
Tod Wood 2017년 2월 4일
B=[3;4;6;9]*29.17658137; B_round_25=round(4*B)/4;

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

채택된 답변

Friedrich
Friedrich 2011년 8월 25일
Hi,
I think you are looking for:
in = [1373.698
1385.024
1394.82
1400.436
1396.532];
%round up
up = floor(in) + ceil( (in-floor(in))/0.25) * 0.25
%round down
down = floor(in) + floor( (in-floor(in))/0.25) * 0.25
  댓글 수: 2
Srinivas
Srinivas 2011년 8월 25일
Thanks :)
Andrei Bobrov
Andrei Bobrov 2011년 8월 25일
bsxfun(@plus,fix(in)+floor(rem(in,1)/.25)*.25,[0 .25])

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 8월 25일
The previous answers look more complex than needed
%round up
up = ceil(in * 4) / 4;
%round down
down = floor(in * 4) / 4;
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2011년 8월 25일
+1
SL
SL 2016년 10월 25일
편집: SL 2016년 10월 25일
Can you make this a single function, please? - - It would be then a complete answer to the question, at least for me. I extended my case here https://se.mathworks.com/matlabcentral/answers/309097-how-to-round-up-to-closest-0-0-or-0-5-in-array

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


Anders Tagmose Lundsfryd
Anders Tagmose Lundsfryd 2021년 3월 17일
편집: Anders Tagmose Lundsfryd 2021년 4월 6일
If you want to round to the nearest 0.25 both up and down, then do:
%roudn to nearest 0.25
updown = round(in * 4)/4;

카테고리

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