查看单个帖子
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何使用带有常量额外输入参数的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)副本的中间单元格数组?



回答:

您可以使用匿名函数来执行此操作,该函数使用另外两个参数调用myfun :

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

更多&回答...
poster 当前离线   回复时引用此帖