For-loop show an element which has format number different with others in an array?
1 次查看(过去 30 天)
显示 更早的评论
Hi,
Could someone explain a strange thing happend in my simple code?
numwl='11';
chieuchimd='1';
dwl=str2double(chieuchimd)/(str2double(numwl)-1);
mwl=[0:dwl:str2double(chieuchimd)]*1000;
text3={};
for i=1:str2double(numwl)
text3=[text3;{sprintf('%d',i-1)} {sprintf('DN%d',i-1)} {sprintf('%d',mwl(i))}]
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145750/image.png)
When for-loop at i=4, the 3rd colum of the cell had a number that its format different with others.
I also search but had no answers.
Thanks
0 个评论
采纳的回答
Thorsten
2014-10-17
This is because mwl is not exactly 300 but 300 + 5.6843e-014.
Either use
mwl = round([0:dwl:str2double(chieuchimd)]*1000);
or
mwl = [0:dwl*1000:str2double(chieuchimd)*1000];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!