cellfun for function with multiple inputs

24 次查看(过去 30 天)
I want to apply a function to every cell within a cell array, so using cellfun seems the logical way to go. But the function requires two inputs, one would be the cell array,A, and the other is B, a 41x1 matrix. So the function would 'apply' B to each cell within A and output the resultant arrays into another cell array
C = cellfun(@func, B, A, 'UniformOutput', false);
Is what I've been doing but it returns the error:
Input #3 expected to be a cell array, was double instead.
Can someone explain how cellfun works for functions with multiple inputs?
  4 个评论
Adam
Adam 2018-1-5
I am assuming that func is a function of 2 arguments, given how you are attempting to use it. Since you are using cellfun I assume the first of those arguments is a single element of the matrix A. Given what you say about applying the whole of B to each element of A I am assuming that the 2nd argument of func is supposed to be the whole of B.
In function handle definitions like this you can have two types of argument. Those that are fixed at the time you define the function handle and those that are variable. B is fixed in my example (i.e. it should be in the workspace at the point the function is definied). a is a placeholder for a variable argument which will be supplied later.
'Later' in your case means in the cellfun statement, where each element of the cell array will successively be given to that function as the 'a' argument.
mathman
mathman 2018-1-5
Okay I understand now. Thanks for the explanation!

请先登录,再进行评论。

采纳的回答

Birdman
Birdman 2018-1-5
编辑:Birdman 2018-1-5
Isn't the error obvious about what you should be doing?
Input #3 expected to be a cell array, was double instead.
Your third input should also be a cell array, but it is double. So convert your double array to cell array by mat2cell, and then apply the cellfun.
  6 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by