6

我将进行一组实验。评估的主要方法具有以下签名:

[Model threshold] = detect(...
    TrainNeg, TrainPos, nf, nT, factors, ...
    removeEachStage, applyEstEachStage, removeFeatures);

其中removeEachStage, applyEstEachStage, 和removeFeatures是布尔值。您可以看到,如果我颠倒任何这些布尔参数的顺序,我可能会得到错误的结果。

MATLAB 中是否有一种方法可以更好地组织以最小化这种错误?或者有什么工具可以用来保护我免受这些错误的影响?

4

1 回答 1

6

有结构的组织

您可以输入struct具有这些参数的字段作为字段。

例如具有字段的结构

setts.TrainNeg
     .TrainPos
     .nf
     .nT
     .factors
     .removeEachStage
     .applyEstEachStage
     .removeFeatures

这样,当您设置字段时,就可以清楚地知道该字段是什么,这与您必须记住参数顺序的函数调用不同。

然后你的函数调用变成

[Model threshold] = detect(setts);

你的函数定义就像

function [model, threshold] = detect(setts)

然后只需将出现的 eg 替换paramsetts.param

混合方法

如果您愿意,也可以将这种方法与您当前的方法混合使用,例如

[Model threshold] = detect(in1, in2, setts);

如果您仍想明确包含in1and in2,并将其余部分捆绑到setts.

面向对象的方法

另一种选择是将检测变成一个类。这样做的好处是detect对象将具有具有固定名称的成员变量,而不是结构,如果在设置字段时打错字,您只需创建一个名称拼写错误的新字段。

例如

classdef detect()
properties
  TrainNeg = [];
  TrainPos  = [];
  nf = [];
  nT = [];
  factors = [];
  removeEachStage = [];
  applyEstEachStage = [];
  removeFeatures =[];
end
methods
  function run(self)
    % Put the old detect code in here, use e.g. self.TrainNeg to access member variables (aka properties)
  end
end
于 2012-08-10T18:24:38.150 回答