Find the nearest whole number and whole number+/-0.5 for a given value

10 ビュー (過去 30 日間)
A
A 2016 年 1 月 18 日
回答済み: John BG 2016 年 1 月 18 日
Hi guys,
I have a simple example. If I have a value:
value = 0.73;
What I want to do is to automatically find what the nearest whole number is +/- 0.5 for a given value. So for example, I want to have something like this:
v0 = 0.73;
v1 = some code; %Tells me 0.5
v2 = some code; %Tells me 1.0
Does this make sense? Here's another example:
v0 = 102.21;
v1 = some code; %Tells me 102.0
v2 = some code; %Tells me 102.5
Thank you!

回答 (2 件)

jgg
jgg 2016 年 1 月 18 日
編集済み: jgg 2016 年 1 月 18 日
You want
v2 = ceil(2*v0)/2
v1 = floor(2*v0)/2
The "round to the closest value" is
round(2*v_0)/2

John BG
John BG 2016 年 1 月 18 日
may be you find the following useful:
v0=100*rand
v0_floor=floor(v0)
v0_ceil=ceil(v0)
v5=.5*(v0_floor+v0_ceil)
if v0<v5
v1=v0_floor;v2=v5
else
v1=v5;v2=v0_ceil
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by