cellfun
将功能应用于单元阵列中的每个单元格
句法
[A1,...,AM] = CellFun(Func,C1,...,CN)
[[一种1,,,,。。。,,,,一种m] = CellFun(功能,,,,C1,,,,。。。,,,,Cn,,,,Name,Value)
description
[[
调用函数句柄指定的函数一种1,,,,。。。,,,,一种m
] = CellFun(功能
,,,,C1,...,CN
)功能
and passes elements from cell arraysC1,...,CN
, 在哪里n
是功能的输入数量功能
。输出数组一种1,,,,。。。,,,,一种m
, 在哪里m
是功能的输出数量功能
,,,,contain the combined outputs from the function calls. The一世
迭代对应于语法[[一种1((一世),,,,。。。,,,,一种m((一世)] = func(C1{i},...,Cn{i})
。这cellfun
功能tion does not perform the calls to function功能
按照特定顺序。
[[
呼叫功能一种1,,,,。。。,,,,一种m
] = CellFun(功能
,,,,C1,...,CN
,,,,名称,价值
)功能
with additional options specified by one or more名称,价值
pair arguments. Possible values for姓名
是“统一输出”
or'ermorhandler'
。
输入参数
|
处理接受的功能 如果功能 |
|
包含的单元阵列 |
姓名-Value Pair Arguments
指定可选的逗号分隔对名称,价值
arguments.姓名
是参数名称和Value
是the corresponding value.姓名
must appear inside single quotes (''
)。您可以按任何顺序指定几个名称和值对参数姓名1,,,,Value1,...,NameN,ValueN
。
|
逻辑值,如下:
默认: |
||||||
|
处理捕获MATLAB尝试执行函数时发生任何错误的函数 MATLABcalls the specified error-handling function with two input arguments:
|
输出参数
|
收集的阵列 Function
|
Examples
计算单元阵列中每个向量的平均值C
。
C= {1:10, [2; 4; 6], []}; averages = cellfun(@mean, C)
此代码返回
averages = 5.5000 4.0000 NaN
Compute the size of each array inC
,,,,created in the previous example.
[nrows,ncols] = cellfun(@size,c)
此代码返回
nrows = 1 3 0 ncols = 10 1 0
创建一个包含字符矢量的单元格数组,并将它们缩写为前三个字符。因为输出字符向量是非尺度的,请设置UniformOutput
到错误的
。
days = {'星期一','星期二','星期三','星期四','星期五'};abbrev = cellfun(@(x)x(1:3),天,'Uniformoutput',false)
语法@(X)
creates an anonymous function. This code returns
abbrev ='mon''tue''wed'thu''fri'
Compute the covariance between arrays in two cell arraysC
andd
。Because the covariance output is nonscalar, setUniformOutput
到错误的
。
c1 = rand(5,1);c2 = rand(10,1);c3 = rand(15,1);d1 = rand(5,1);d2 = rand(10,1);d3 = rand(15,1);C = {C1,C2,C3};d = {d1,d2,d3};covcd = cellfun(@cov,c,d,'simurencoutOutput',false)
此代码返回
covCD = [2x2 double] [2x2 double] [2x2 double]
定义并调用自定义错误处理功能。
函数结果= errorfun(s,varargin)警告(S.Identifier,s.message);结果= nan;结束a = {rand(3)};b = {rand(5)};agtb = cellfun( @(x,y)x> y,a,b,'ermorhandler', @errorfun,...'simurancoutOutput',false)