MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在MATLAB中使用匿名函数跳过输出 (https://www.labfans.com/bbs/showthread.php?t=23553)

poster 2019-12-10 20:41

在MATLAB中使用匿名函数跳过输出
 
假设我想从返回两个输出的m文件函数创建一个匿名函数。是否可以设置匿名函数,使其仅返回m文件功能的第二个输出?

示例: ttest2返回两个输出,t / f和一个概率。如果我想对cellfun使用t检验,我可能只对收集概率感兴趣,即我想写这样的东西

probabilities = cellfun(@(u,v)ttest2(u,v)%take only second output%,cellArray1,cellArray2)

[B]回答:[/B]

我无法在[URL="http://www.mathworks.co.uk/access/helpdesk/help/techdoc/matlab_prog/f4-70115.html"]匿名函数[/URL] [I]的表达式[/I]中知道让它选择从具有多个可能输出参数的函数返回哪个输出。但是,当您[I]评估[/I]匿名函数时,您[I]可以[/I]返回多个输出。这是使用函数[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/max.html"]MAX[/URL]的示例:

>> data = [1 3 2 5 4]; %# Sample data >> fcn = @(x) max(x); %# An anonymous function with multiple possible outputs >> [maxValue,maxIndex] = fcn(data) %# Get two outputs when evaluating fcn maxValue = 5 %# The maximum value (output 1 from max) maxIndex = 4 %# The index of the maximum value (output 2 from max) 此外,为了处理您在上面给出的具体例子中,最好的办法是实际上只是使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/function_handle.html"]功能手柄[/URL] @ttest2作为输入到[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"]CELLFUN[/URL] ,然后从获得多个输出[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/cellfun.html"]CELLFUN[/URL]本身:

[junk,probabilities] = cellfun(@ttest2,cellArray1,cellArray2); 在较新版本的MATLAB上,可以将变量junk替换为[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1"]~[/URL]以忽略第一个输出参数。



[url=https://stackoverflow.com/questions/3096281]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 01:08

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.