Converting variable length vector into variable length input argument for function

3 ビュー (過去 30 日間)
I'm trying to turn a function output vector of variable length into a variable number of arguments to be fed into another function, so each element of the output vector becomes one input argument for the next function below:
function [pd]=fit(xdata,fitname,pdfunc,start)
[params]=mle(xdata, 'pdf',pdfunc, 'start',start);
pd=makedist(fitname,deal(num2cell(params)));
end
xdata is some data array, fitname is something like 'Exponential' or 'GeneralizedPareto', pdfunc is an anonymous function such as pdfgp=@(x,k,sigma,theta) gppdf(x,k,sigma,theta), and start is the initial parameter values required by mle.
Right now this produces a "Not enough input arguments" error. I've also tried variants on using eval, deal and num2cell with no luck. Any help would be appreciated. Thanks!

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 24 日
This will not do any good. The output of mle is a numeric parameter estimate scalar or row vector. The parameters of makedist must be first the fitting name, and then name/value pairs.
You can use something like
temp = num2cell(params);
param_names = {'mu', 'sigma'};
pv_pairs = [param_names; temp];
pd = makedist(fitname, pv_pairs{:});
This counts on cell expansion going down columns first.
  1 件のコメント
dandan
dandan 2018 年 1 月 25 日
Makedist actually does accept the parameter values without names (at least in my 2016b version of Matlab). For example,
pd = makedist('GeneralizedPareto',1,1,0)
appears to work just as well as
pd = makedist('GeneralizedPareto','k',1,'sigma',1,'theta',0)
Based on your suggestion though, I realized I was just way overcomplicating it, and it turns out
temp=num2cell(params);
pd = makedist(fitname,temp{:});
works perfectly! Thank you!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by