Map function in MATLAB?

前端 未结 7 835
情书的邮戳
情书的邮戳 2020-11-29 17:11

I\'m a little surprised that MATLAB doesn\'t have a Map function, so I hacked one together myself since it\'s something I can\'t live without. Is there a better version out

7条回答
  •  天命终不由人
    2020-11-29 18:14

    In addition to vector and element-wise operations, there's also cellfun for mapping functions over cell arrays. For example:

    cellfun(@upper, {'a', 'b', 'c'}, 'UniformOutput',false)
    ans = 
        'A'    'B'    'C'
    

    If 'UniformOutput' is true (or not provided), it will attempt to concatenate the results according to the dimensions of the cell array, so

    cellfun(@upper, {'a', 'b', 'c'})
    ans =
    ABC
    

提交回复
热议问题