Matlab integer format for answer
9 次查看(过去 30 天)
显示 更早的评论
How do I get matlab to output the answer of (2+24)/(4*3) as an integer? It keps giving the answer as a decimal
2.166666666666667
0 个评论
采纳的回答
Image Analyst
2013-2-18
Try a function, like int32(), fix(), ceil(), floor(), uint8(), int32(round()), etc.
a = (2+24)/(4*3) % Floating point.
aInteger = int32(a)
whos aInteger; % int32
aInteger = round(a)
whos aInteger; % Double
aInteger = floor(a)
whos aInteger; % Double
aInteger = ceil(a)
whos aInteger; % Double
aInteger = fix(a)
whos aInteger; % double
aInteger = uint16(a)
whos aInteger; % uint16
Note that with round(), ceil(), floor(), and fix() you will end up with a double but with no fractional part. If you want an actual integer, you have to cast to an integer data type with int32(), int16(), etc.
0 个评论
更多回答(1 个)
Thorsten
2013-2-18
Because the result is not an integer. To convert from decimal to integer, use one of these functions:
round, floor, ceil
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!