【问题标题】:Matlab comparation of two numbers not working [duplicate]Matlab比较两个数字不起作用[重复]
【发布时间】:2013-07-26 21:48:22
【问题描述】:

我正在尝试比较 if 语句中的三个数字,但即使它们相同,它们也不匹配。

这里是命令窗口中的数字,

>> a = (round2(final_aucscore(10, 1),1e-4))

a =

    0.9369

>> b = (round2(final_aucscore(10, 2),1e-4))

b =

    0.9598

>> c = (round2(final_aucscore(10, 3),1e-4))

c =

    0.9509

函数round2可以在文件echangehere中找到。

这是我的代码:

for mmm = 1:265

    a = (round2(final_aucscore(mmm, 1),1e-4));
    b = (round2(final_aucscore(mmm, 2),1e-4));
    c = (round2(final_aucscore(mmm, 3),1e-4));
    if   a == 0.9369 && b == 0.9598 && c == 0.9509
        auc_idx = idx(1:kk);
        save('auc_idx', 'auc_idx', 'mmm');
        break;
    end
end

mmm = 10时不应该停止保存吗?

【问题讨论】:

  • FEX 上的 round2 功能很糟糕(甚至有 4.8 分!)。它甚至没有矢量化。 这样的事情不应该被允许。它可以被这个匿名函数替换:round2 = @(x,y)round(x./y).*y; 或者你可以编辑代码。但是,它仍然会受到浮点不精确的影响。
  • This function on the FEX,也称为round2,在数值上似乎要好得多(虽然仅评级为 4.3...)。

标签: matlab if-statement for-loop


【解决方案1】:

您在这里面临的问题来自舍入误差。 试试这个

foo = 0.9369;
bar = (round2(foo,1e-4));
disp(bar == foo)
disp(foo-bar)

您可以改为创建一个仅在特定精度范围内进行比较的函数。

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多