MATLAB:有没有一种方法可以更好地组织实验函数? [英] MATLAB: Is there a method to better organize functions for experiments?

查看:26
本文介绍了MATLAB:有没有一种方法可以更好地组织实验函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I will run a set of experiments. The main method evaluated has the following signature:

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

其中 removeEachStageapplyEstEachStageremoveFeatures 是布尔值.你可以看到,如果我颠倒这些布尔参数中的任何一个的顺序,我可能会得到错误的结果.

where removeEachStage, applyEstEachStage, and removeFeatures are booleans. You can see that if I reverse the order of any of these boolean parameters I may get wrong results.

MATLAB 中是否有一种方法可以更好地组织以最大程度地减少此类错误?或者有什么我可以使用的工具来保护我免受这些错误的影响?

Is there a method in MATLAB that allows better organization in order to minimize this kind of error? Or is there any tool I can use to protect me against these errors?

推荐答案

组织结构

您可以输入一个 struct ,它具有这些参数作为它的字段.

You could input a struct that has these parameters as it's fields.

例如带有字段的结构

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

这样,当您设置字段时,字段是什么就一目了然,这与函数调用不同,您必须记住参数的顺序.

That way when you set the fields it is clear what the field is, unlike a function call where you have to remember the order of the parameters.

那么你的函数调用就变成了

Then your function call becomes

[Model threshold] = detect(setts);

你的函数定义类似于

function [model, threshold] = detect(setts)

然后简单地替换例如的出现paramsetts.param.

Then simply replace the occurrences of e.g. param with setts.param.

混合方法

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

You can also mix this approach with your current one if you prefer, e.g.

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

如果您仍想明确包含 in1in2,并将其余部分捆绑到 sets 中.

if you wanted to still explicitly include in1 and in2, and bundle the rest into setts.

面向对象的方法

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

Another option is to turn detect into a class. The benefit to this is that a detect object would then have member variables with fixed names, as opposed to structs where if you make a typo when setting a field you just create a new field with the misspelled name.

例如

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

这篇关于MATLAB:有没有一种方法可以更好地组织实验函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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