MATLAB:是否有更好的组织功能实验的方法? [英] MATLAB: Is there a method to better organize functions for experiments?
问题描述
我将运行一组实验。评估的主要方法有以下签名:
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);
其中, removeEachStage
, applyEstEachStage
和 removeFeatures
是布尔值。你可以看到,如果我扭转这些布尔参数的顺序我可能得到错误的结果。
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?
推荐答案
组织与结构
您可以输入结构
有这些参数,因为它的领域。
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.
那么你的函数调用变得
[Model threshold] = detect(setts);
和你的函数定义是类似
function [model, threshold] = detect(setts)
然后,只需替换例如发生参数
与 setts.param
。
混合模式
您也可以与您当前的一个,如果你preFER,例如混合了这种方法。
You can also mix this approach with your current one if you prefer, e.g.
[Model threshold] = detect(in1, in2, setts);
如果你想还明确包括 IN1
和平方英寸
,并捆绑其余为小方石
。
if you wanted to still explicitly include in1
and in2
, and bundle the rest into setts
.
OOP方法
另一种选择是把检测到的一类。这样做的好处是,检测
对象,然后将有成员变量固定的名称,而不是结构的地方,如果你设置一个字段时,发现错字,你只需创建一个新的场与拼写错误的名称。
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屋!