필터 지우기
필터 지우기

How to round up to closest 0 or 5 in array?

조회 수: 3 (최근 30일)
SL
SL 2016년 10월 25일
편집: SL 2016년 10월 25일
Code
utp = [420.680537950975, 412.407955038480, 389.575547217989, 366.089887932711, 363.090652446469, 352.095749639858];
% https://se.mathworks.com/matlabcentral/answers/14495-rounding-elements-in-array-to-nearest-0-25
up = ceil(utp * 2) / 2;
down = floor(utp * 2) / 2;
Expected output
420
410
390
365
365
350
I started to think to make an for-loop for the task but really - it does not feel like MATLAB.
Maybe, there is something built-in.
MATLAB: 2016b OS: Debian 8.5

답변 (1개)

Walter Roberson
Walter Roberson 2016년 10월 25일
  댓글 수: 2
John D'Errico
John D'Errico 2016년 10월 25일
편집: John D'Errico 2016년 10월 25일
oops. To round to the nearest multiple of 5, I expect you meant to write:
round(utp/5)*5
SL
SL 2016년 10월 25일
편집: SL 2016년 10월 25일
Yes, there is a typo in Walter's answer. John's correction is valid. It would be great to get some explanation in your answer such that it would be more generic. Now, I do for instance this 1 `utp = utp * 1000; utp = round(utp/5)*; utp = utp / 1000;` to get dimensions match in cases where integers are not significant. Maybe, just add a notification that it is designed for integers is the best option here.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by