文档

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'

输入参数

功能

处理接受的功能n输入参数和返回moutput arguments.

如果功能功能corresponds to more than one function file (that is, if功能代表一组超载功能),MATLAB®根据输入参数的类确定要调用的函数。

Backward Compatibility

C1,...,CN

包含的单元阵列n功能所需的输入功能。每个单元阵列必须具有相同的维度。

姓名-Value Pair Arguments

指定可选的逗号分隔对名称,价值arguments.姓名是参数名称和Value是the corresponding value.姓名must appear inside single quotes ('')。您可以按任何顺序指定几个名称和值对参数姓名1,,,,Value1,...,NameN,ValueN

“统一输出”

逻辑值,如下:

true (1)

表示对于所有输入,每个输出都来自函数功能是标量单元格数组,标量结构或始终具有相同类型和大小的标量值。这cellfun功能将输出组合在数组中一种1,,,,。。。,,,,一种m, 在哪里m是the number of function outputs. Each output array is of the same type as the individual function outputs.

假(0)

Requests that thecellfun功能将输出组合到单元阵列中一种1,,,,。。。,,,,一种m。功能的输出功能可以具有任何尺寸或类型。

默认:true

'ermorhandler'

处理捕获MATLAB尝试执行函数时发生任何错误的函数功能。define this function so that it rethrows the error or returns valid outputs for function功能

MATLABcalls the specified error-handling function with two input arguments:

  • 一种structure with these fields:

    标识符

    Error identifier.

    信息

    错误消息文本。

    指数

    Linear index corresponding to the element of the input cell array at the time of the error.

  • 这set of input arguments to function功能在错误时。

输出参数

一种1,,,,。。。,,,,一种m

收集的阵列m功能输出功能。每个数组一种是the same size as each of the inputsC1,...,CN

Function功能can return output arguments of different classes. However, ifUniformOutputtrue(默认):

  • 这一世ndividual outputs from function功能must be scalar values (numeric, logical, or character), scalar structures, or scalar cell arrays.

  • 对于每组输入,特定输出参数的类别必须相同。相应输出数组的类与功能的输出类相同功能

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 arraysCandd。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)

扩展功能

Introduced before R2006a

Was this topic helpful?