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
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

채택된 답변

Thorsten
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];
  댓글 수: 1
Khanh
Khanh 2014년 10월 20일
Thank you so much. My number array is exactly with interval is 100 (0:100:1000). But I also solved the problem by using your code. Thanks again :D.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by