如何使用常量额外输入参数应用cellfun(或arrayfun或structfun)? [英] How to apply cellfun (or arrayfun or structfun) with constant extra input arguments?

查看:444
本文介绍了如何使用常量额外输入参数应用cellfun(或arrayfun或structfun)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个函数应用于单元格数组的每个元素 - 因此我有 cellfun 。然而,该函数需要两个额外的参数(一个字符串和一个向量),我希望对单元格数组的所有元素保持不变;即我想要做的事情:

I want to apply a function to each element of a cell array -- so I have cellfun for that. However, the function takes two extra arguments (a string and a vector), which I want to keep constant for all the elements of the cell array; i.e. I'd like to do something like:

cellfun(@myfun, cellarray, const1, const2)

含义:

meaning:

for i = 1:numel(cellarray),
  myfun(cellarray{i}, const1, const2);
end

有没有办法创建包含<$ c的中间单元格数组$ c> numel(cellarray) const1 const2

Is there some way to do that without creating intermediate cell arrays containing numel(cellarray) copies of const1 and const2?

推荐答案

您可以使用匿名函数,它使用另外两个参数调用 myfun

You can do this using an anonymous function that calls myfun with the two additional arguments:

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

这篇关于如何使用常量额外输入参数应用cellfun(或arrayfun或structfun)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆