登录论坛

查看完整版本 : 如何使用带有常量额外输入参数的cellfun(或arrayfun或structfun)?


poster
2019-12-10, 20:48
我想将一个函数应用于单元格数组的每个元素-所以我cellfun提供了cellfun 。但是,该函数需要两个额外的参数(字符串和向量),我想对单元格数组的所有元素保持不变;即我想做类似的事情:

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

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



回答:

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

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

更多&回答... (https://stackoverflow.com/questions/3281575)