将参数传递给Matlab函数

时间:2015-06-20 09:20:49

标签: matlab

我有一个非常简单的问题,但我没有弄清楚如何解决这个问题。我有下面的函数定义:

function model = oasis(data, class_labels, parms)
% model = oasis(data, class_labels, parms)
%
% Code version 1.3 May 2011 Fixed random seed setting
% Code version 1.2 May 2011 added call to oasis_m.m
% Code version 1.1 May 2011 handle gaps in class_labels
% 
%  Input:
%   -- data         - Nxd sparse matrix (each instance being a ROW)
%   -- class_labels - label of each data point  (Nx1 integer vector)
%   -- parms (do sym, do_psd, aggress etc.)
% 
%  Output: 
%   -- model.W - dxd matrix
%   -- model.loss_steps - a binary vector: was there an update at
%         each iterations
%   -- modeo.parms, the actual parameters used in the run (inc. defaults)
% 
%  Parameters:
%   -- aggress: The cutoff point on the size of the correction
%         (default 0.1) 
%   -- rseed: The random seed for data point selection 
%         (default 1)
%   -- do_sym: Whether to symmetrize the matrix every k steps
%         (default 0)
%   -- do_psd: Whether to PSD the matrix every k steps, including
%         symmetrizing them (defalut 0)
%   -- do_save: Whether to save the intermediate matrices. Note that
%         saving is before symmetrizing and/or PSD in case they exist
%         (default 0)
%   -- save_path: In case do_save==1 a filename is needed, the
%         format is save_path/part_k.mat
%   -- num_steps - Number of total steps the algorithm will
%         run (default 1M steps)
%   -- save_every: Number of steps between each save point
%         (default num_steps/10)
%   -- sym_every: An integer multiple of "save_every",
%         indicates the frequency of symmetrizing in case do_sym=1. The
%         end step will also be symmetrized. (default 1)
%   -- psd_every: An integer multiple of "save_every",
%         indicates the frequency of projecting on PSD cone in case
%         do_psd=1. The end step will also be PSD. (default 1)
%   -- use_matlab: Use oasis_m.m instead of oasis_c.c
%      This is provided in the case of compilation problems.
% 

我想使用此功能,但我不知道如何设置参数,或使用默认值。在这种情况下,变量parms是什么,它是一个保留所有其他变量的对象?我可以创建一些类似python的语法,我们将参数的名称加上值吗?例如:

model = oasis(data_example, labels_example, agress = 0.2)

另外,如果我理解正确,我在输出中得到两个对象,即模型和模式,所以我需要进行此调用以接收此函数返回的所有内容?

[model,modeo] = oasis(data_example, labels_example, ?(parms)?)

2 个答案:

答案 0 :(得分:1)

从您的函数定义来看,params似乎只是参数的占位符。通常,参数本身作为输入对传递:

model = oasis(data, class_labels, 'do_sym',do_symValue, 'do_psd', do_psdValue,...)

其中do_symValuedo_psdValue是您要作为相应参数传递的值。

对于函数返回值,它返回一个struct个成员Wloss_stepsparms。我相信你认为的第二个输出(modelo)只是文本中的拼写错误 - 至少基于函数的定义。

答案 1 :(得分:0)

从上面的文档中,我不知道哪一个是正确的,但matlab中有两种常用的可选参数方法。

参数值对:​​

model = oasis(data, class_labels, 'do_sym',1,'do_psd',0)

结构:

params.do_sym=1
params.do_psd=0
model = oasis(data, class_labels, params)

这两种可能性中的一种可能是正确的。