MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何使用带有常量额外输入参数的cellfun(或arrayfun或structfun)? (https://www.labfans.com/bbs/showthread.php?t=23713)

poster 2019-12-10 20:48

如何使用带有常量额外输入参数的cellfun(或arrayfun或structfun)?
 
我想将一个函数应用于单元格数组的每个元素-所以我cellfun提供了cellfun 。但是,该函数需要两个额外的参数(字符串和向量),我想对单元格数组的所有元素保持不变;即我想做类似的事情:

cellfun(@myfun, cellarray, const1, const2) 含义:

for i = 1:numel(cellarray), myfun(cellarray{i}, const1, const2); end 有没有一种方法可以创建不包含const1和const2 numel(cellarray)副本的中间单元格数组?



[B]回答:[/B]

您可以使用[URL="http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html"]匿名函数[/URL]来执行此操作,该[URL="http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html"]函数[/URL]使用另外两个参数调用myfun :

cellfun(@(x) myfun(x,const1,const2), cellarray)

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


所有时间均为北京时间。现在的时间是 14:17

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