フィルターのクリア

Round to nearest odd integer

148 ビュー (過去 30 日間)
Caleb
Caleb 2012 年 8 月 13 日
編集済み: Walter Roberson 2019 年 12 月 30 日
Does MATLAB have a function to round numbers to the nearest odd integer?
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 8 月 14 日
Do you want negative numbers handled the same way? e.g., -1/3 rounded to -1 ?

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 8 月 13 日
編集済み: Matt Fig 2012 年 8 月 13 日
I don't think so, but you could make your own:
function S = round_odd(S)
% round to nearest odd integer.
idx = mod(S,2)<1;
S = floor(S);
S(idx) = S(idx)+1;
  2 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 8 月 13 日
I think you meant:
idx = mod(S,2)<1 %note < not <=
Otherwise it returns evens for S = magic(5);
Matt Fig
Matt Fig 2012 年 8 月 13 日
Yeah, I caught that one too. I like Babak's better anyway, LOL.

サインインしてコメントする。

その他の回答 (4 件)

Babak
Babak 2012 年 8 月 13 日
編集済み: Walter Roberson 2019 年 12 月 30 日
Not that I know of.. but you can use this:
function y=fun(x)
y = 2*floor(x/2)+1;
end
depending on what you want to do with the even numbers you can ether use floor or ceil in the above function.
  2 件のコメント
mirza
mirza 2019 年 12 月 29 日
That's an efficient approach, but its better to use "round" function instead of "floor" or "ceiling" function. In floor case, the error may occur for rounding numbers to Even.
For example.
If the number is " 23.7227731 "
then statement => 2*floor(x/2) will provide answer "22"
However,
the statement => 2*round(x/2) will provide answer "24"
Walter Roberson
Walter Roberson 2019 年 12 月 30 日
2*floor(23.7227731/2)+1 would be 23, and 23.7227731 has indeed been rounded to the nearest odd number.
2*round(23.7227731/2)+1 would be 25, but 25-23.7227731 is about 1.67 which is greater than 23.7227731-23 ~= 0.72 . Therefore 23 is the closest odd number, to 23.722773, and 25 is not the closest odd number.
I would therefore suggest to you that round() does not solve the problem posed by the original poster.

サインインしてコメントする。


Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 13 日
a=7.8; %example
round((a-1)/2)*2+1
  3 件のコメント
mirza
mirza 2019 年 12 月 29 日
True, the function "round" is better option instead of functions "floor" or "ceiling" as suggested in thread. In floor case, the error may occur for rounding numbers to Even.
For example.
If the number is " 23.7227731 "
then statement => 2*floor(x/2) will provide answer "22"
However,
the statement => 2*round(x/2) will provide answer "24"
Walter Roberson
Walter Roberson 2019 年 12 月 30 日

サインインしてコメントする。


gerit
gerit 2018 年 9 月 12 日
編集済み: Walter Roberson 2018 年 9 月 15 日
x-mod(x,2)+1
even numbers will be rounded up

Gimantha Upasena
Gimantha Upasena 2015 年 4 月 21 日
編集済み: Gimantha Upasena 2015 年 4 月 21 日
In matlab what is the method to take the values of a matrix and round them off to the nearest even number?..... meaning 2.1 should be rounded off to 2 and also 2.8 should also be rounded off to 2 because if rounded off to 3 ...3 is an odd number so it's not accepted.
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 12 月 30 日
編集済み: Walter Roberson 2019 年 12 月 30 日
floor(x/2)*2
which should work for both positive and negative numbers.

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by