MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   [MATLAB基础] matlab的一点小疑惑 (https://www.labfans.com/bbs/showthread.php?t=17878)

zhangyunjiea 2012-11-19 12:00

matlab的一点小疑惑
 
clear
B=1:0.01:6;
for i=0:0.01:5
B(i*100+1)=i;
end
运行结果
??? Attempted to access B(15); index must be a positive integer or logical
请问错在哪?为什么啊?

bigfrog 2012-11-21 12:50

回复: matlab的一点小疑惑
 
索引必须是整型,由于小数的计算机存储截断问题,i从0到0.13,i*100+1的结果小数部分为0(准确点是无限小),当i=0.14时,实际i*100+1=15.000000000000002,因此会报错。建议强制转换成整型,如uint32(i*100+1)。


所有时间均为北京时间。现在的时间是 05:42

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.