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

查看:28
本文介绍了如何应用带有常量额外输入参数的 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)

含义:

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

有没有办法在不创建包含 numel(cellarray) 副本的 const1const2 的中间元胞数组的情况下做到这一点?

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天全站免登陆