打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
matlab下判断是否是整数。
饮水思源 - 主题文章阅读  [讨论区: MathTools]本主题共有 4 篇文章,分 1 页, 当前显示第 1 页 [返回讨论区]
[回复本文][原帖] 发信人:visame(Visame), 信区: MathTools 标 题: matlab下判断是否是整数。 发信站: 饮水思源 (2008年11月19日04:32:01 星期三), 站内信件 经尝试,使用isinteger()函数是行不通的 一段程序,想在x为整数时,相应y的值为2 x为非整数时,相应y的值为0 我用x==round(x)或者abs(x-round(x))<=esp判断x是否为整数都无法得到正确的答案 中间有一段x本来是整数【-24到24之间】却无法判断出来 特来求教 x=-100.3:0.1:100.3; %x=-50.3:0.1:50.3; %x=-24.0:0.1:24.0; y=zeros(1,length(x)); m=[]; for i=1:length(x) if abs(x(i)-round(x(i)))<=eps m=[m,x(i)]; y(i)=2; end end m记录的值如下,缺了-24到24之间 本人使用盗版Matlab,不知是否与此有关 m = Columns 1 through 14 -100 -99 -98 -97 -96 -95 -94 -93 -92 -91 -90 -89 - 88 -87 Columns 15 through 28 -86 -85 -84 -83 -82 -81 -80 -79 -78 -77 -76 -75 - 74 -73 Columns 29 through 42 -72 -71 -70 -69 -68 -67 -66 -65 -36 -35 -34 -33 - 32 -31 Columns 43 through 56 -30 -29 -28 -27 -26 -25 -24 0 24 25 26 27 28 29 Columns 57 through 70 30 31 32 33 34 35 36 65 66 67 68 69 70 71 Columns 71 through 84 72 73 74 75 76 77 78 79 80 81 82 83 84 85 Columns 85 through 98 86 87 88 89 90 91 92 93 94 95 96 97 98 99 Column 99 100 -- 闻道有先后,术业有专攻,如是而已。 "New Term's Resolution: Participate in every SRM in 2008" You may be disappointed if you fail, but you are doomed if you don't try. ※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: Visame.USA.Europe.Tokyo] ※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 202.113.19.246] ※ 修改内容:·visame 于 11月19日04:32:49 修改本文·[FROM: 202.113.19.246]
[回复本文][原帖] 发信人:oldlady(弱), 信区: MathTools 标 题: Re: matlab下判断是否是整数。 发信站: 饮水思源 (2008年11月19日20:54:44 星期三), 站内信件 if floor(x) == x code end 看看这样行不行 【 在 visame (Visame) 的大作中提到: 】 : 经尝试,使用isinteger()函数是行不通的 : 一段程序,想在x为整数时,相应y的值为2 : x为非整数时,相应y的值为0 : 我用x==round(x)或者abs(x-round(x))<=esp判断x是否为整数都无法得到正确的答案 : 中间有一段x本来是整数【-24到24之间】却无法判断出来 : 特来求教 : x=-100.3:0.1:100.3; : %x=-50.3:0.1:50.3; : %x=-24.0:0.1:24.0; : y=zeros(1,length(x)); : .................(以下省略) -- When we rebound,we win ※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 202.120.45.110]
[回复本文][原帖] 发信人:georgewbush(乔治·w·布什), 信区: MathTools 标 题: Re: matlab下判断是否是整数。 发信站: 饮水思源 (2008年11月20日05:29:25 星期四) 问题出在eps上 可能是matlab取整原理问题,不管是round,floor,ceil都一样,取整精度不定。 eps精度太高了,取整近似的结果达不到这个精度 0.1间隔取数,用error=1e-5的精度远远够啦 所以是 abs(x-round(x))<=1e-5 【 在 visame 的大作中提到: 】 : 经尝试,使用isinteger()函数是行不通的 : 一段程序,想在x为整数时,相应y的值为2 : x为非整数时,相应y的值为0 : 我用x==round(x)或者abs(x-round(x))<=esp判断x是否为整数都无法得到正确的答案 : 中间有一段x本来是整数【-24到24之间】却无法判断出来 : 特来求教 : x=-100.3:0.1:100.3; : %x=-50.3:0.1:50.3; : %x=-24.0:0.1:24.0; : y=zeros(1,length(x)); : m=[]; : for i=1:length(x) : if abs(x(i)-round(x(i)))<=eps : m=[m,x(i)]; : y(i)=2; : end : end : m记录的值如下,缺了-24到24之间 : 本人使用盗版Matlab,不知是否与此有关 : m = : (以下引言省略...) -- 退休了,怎地 当今世界上最大的政教合一的国家 还是中国,无奈 ※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 83.84.133.205] ※ 修改内容:·georgewbush 于 11月20日05:39:59 修改本文·[FROM: 83.84.133.205]
[回复本文][原帖] 发信人:tango(糖圈), 信区: MathTools 标 题: Re: matlab下判断是否是整数。 发信站: 饮水思源 (2008年11月20日23:50:25 星期四), 站内信件 This is a good question. And in fact, it touches two dark areas. 1. The floating point precision issue. Due to the limited precision of float ing point, you can not trust such calculation based on 0.1. Try the following 2 in matlab: >> num2hex(0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1), num2hex(1.0) ans = 3fefffffffffffff ans = 3ff0000000000000 2. MATLAB's "intelligence". MATLAB's author has met similar issue from its customers. And the unnecessar y complaint has made MATLAB do some special tricks to bypass these issues. Y ou can see the following article:http://www.mathworks.com/company/newslette rs/news_notes/pdf/Fall96Cleve.pdf Cleve mentioned: MATLAB is careful to arrange that the last element of the vector 0:0.1:1 is exactly equal to 1, In other words, this is not like c/cpp/fortran/..., the MATLAB will do some tricks inside to do the calculation. And if matlab do not release its intern al document on this, no one will know what exactly the loop inner and final value will be. And considering the history of matlab, the software will also not change this behavior. Let's return to your proto question. Since you just want it to be executed f rom -100.3 to 100.3, you can have the easy way to use -1003:1:1003, and let every value to be divided by 10. And this value should be ok for you to use. Don't worry, to such a value in such a range, we can trust matlab. And if yo u still have some concerns, you can use the following way to get the -1003:1 :1003 int32(-1003):int32(1):int32(1003) I think you will definitely satisfied with this. BTW: the isinteger function is used to decide whether the datatype is intege r, not the data value. 【 在 visame (Visame) 的大作中提到: 】 : 经尝试,使用isinteger()函数是行不通的 : 一段程序,想在x为整数时,相应y的值为2 : x为非整数时,相应y的值为0 : 我用x==round(x)或者abs(x-round(x))<=esp判断x是否为整数都无法得到正确的答案 : 中间有一段x本来是整数【-24到24之间】却无法判断出来 : 特来求教 : x=-100.3:0.1:100.3; : %x=-50.3:0.1:50.3; : %x=-24.0:0.1:24.0; : y=zeros(1,length(x)); : .................(以下省略) -- Tango就是趟啊趟着走 ※ 来源:·饮水思源 bbs.sjtu.edu.cn·[FROM: 222.70.235.130]
本主题共有 4 篇文章,分 1 页, 当前显示第 1 页 [返回讨论区]
[返回上一页][本讨论区(一般模式)]
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
matlab?使用的一点儿体会(for?beginner)?(转载)
【转】MATLAB中取整函数(fix,?floor,?ceil,?round)的使用
关于Matlab 和 C的取整操作 来自 木头弯弯的小窝
【设计经验】5、Verilog对数据进行四舍五入(round)与饱和(saturation)截位
matlab一些常用函数总结
Excel表格向上,中,向下取整函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服