Matlab if循环不起作用 [英] Matlab if loop not working

查看:205
本文介绍了Matlab if循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%function [flag] =verify(area)

[FileName,PathName,FilterIndex]= uigetfile('*.tif','Select the signature file');
display(PathName) 
m=[PathName,FileName];
area=nor_area(m);
%display(area)

%area=0.8707;
class(area)

flag=0;
extract=xlsread('D:\Project\Image_processing\important\best.xlsx', 'CW4:CW17');
c=numel(extract);
display(c)
l=extract(1);
class(l)
display(l)
for k = 1:c
%x=extract(k);
if (l==area && flag==0)
     % display(extract(k)); 
      flag=1;
      display(flag)
end 
end
display(flag)

以上是我的验证代码,即使值相同,我也无法比较"l == area".如果我尝试传递值,则假定l = 0.9999,并且我获得的面积相同,如果我显式发送l值,则它起作用..!但是,如果我尝试使用某些功能并传递相同的值,它将无法正常工作.我试过通过使用类检查类型,都返回双.

The above is my code for verification, i am not able to compare "l==area", even if the values are same am not able to enter inside the loop. If i try passing the value assume l=0.9999 and the area that i obtain to be the same , if i sent l value explicitly it works..!! but if i try using some function and pass the same value it wont work. I have tried checking the type by using class, both returns double.

任何人都可以帮我解决这个问题,如果这种方法不好,请提出任何可能使用的替代方法.

Can anyone please help me out with this and if this approach is not good, suggest any alternative that may be used.

推荐答案

比较浮点数(与==运算符)通常不是一个好主意,因为与整数值不同,浮点数会四舍五入.请参见此处此处,用于在MATLAB中比较浮点数.

It is not generally a good idea to compare floats like you are doing (with the == operator) since floats, unlike integer values are subject to round off. See here and here for a discussion on comparing floats in MATLAB.

基本上,您必须检查两个浮子是否足够接近"而不是完全相等,而这正是==所检查的. MATLAB具有内置函数eps,用于确定计算机上的浮点精度,因此在比较浮点数时可以使用该函数.有关更多信息,请参见其文档.

Essentially you have to check that two floats are 'close enough' rather than exactly equal, which is what == checks for. MATLAB has a built in function eps for determining the floating point precision on your machine, so use that function when comparing floats. See its documentation for more information.

这篇关于Matlab if循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆