如何使用`cellfun`(MATLAB)应用具有多个参数的函数?

时间:2015-02-26 13:23:23

标签: matlab image-processing matrix cell-array divide

使用cellfun,如何将函数应用于mat2cell函数创建的所有单元格?我的函数在另一个文件中定义,此处由myFunc引用。该函数有两个参数,应该是一个单元格和一个整数。

e.g。 function H = myFunc(img,Q)

我的代码如下:

% Split into grid and process each cell
width = size(img,2) % get image width
height = size(img,1) % get image height
depth = size(img,3) % get depth
C = mat2cell(img,[height/2 height/2],[width/2 width/2],[depth/2 depth/2]); % divides image into sections
F = cellfun(@myFunc,C);
save(fout,'F');

问题当然在于行F = cellfun(@myFunc,C);。如何传递单元格和选定的整数,例如每个细胞4到myFunc

非常感谢。

2 个答案:

答案 0 :(得分:12)

好吧,只需将新的匿名函数定义为@(x) myFunc(x,4)并以这种方式使用它:

F = cellfun(@(x) myFunc(x,4), C)

答案 1 :(得分:4)

使用匿名函数:

F = cellfun(@(Q) myFunc(Q,4),C);