gpt4 book ai didi

matlab - 如何应用带有常量额外输入参数的 cellfun(或 arrayfun 或 structfun)?

转载 作者:太空宇宙 更新时间:2023-11-03 19:18:48 25 4
gpt4 key购买 nike

我想对元胞数组的每个元素应用一个函数——所以我有 cellfun。但是,该函数需要两个额外的参数(一个字符串和一个向量),我希望对元胞数组的所有元素保持不变;即我想做类似的事情:

cellfun(@myfun, cellarray, const1, const2)

含义:

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

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

最佳答案

您可以使用 anonymous function 来做到这一点用两个附加参数调用 myfun:

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

关于matlab - 如何应用带有常量额外输入参数的 cellfun(或 arrayfun 或 structfun)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3281575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com