cellfun for function with multiple inputs

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 日
If you want to apply the entirety of B to each element of A then B should be a pre-defined argument to func
e.g.
f = @(a) func( a, B )
C = cellfun( f, A, 'UniformOutput', false );
mathman
mathman 2018 年 1 月 5 日
Just to make sure I'm understanding this correctly, f is used to define a 'variable' which contains a function with one input into it already, which is then used in cellfun? And could you explain what the lowercase 'a' is corresponding to?
Thanks alot
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 日

0 投票

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 件のコメント

mathman
mathman 2018 年 1 月 5 日
Is it not possible to keep it as a double?
I'm not clear on how dividing the matrix into cells would impact the final requirement.
Birdman
Birdman 2018 年 1 月 5 日
Share your variables and I can be more helpful. Also I shared the reference page for mat2cell, but it seems that you did not even open it.
mathman
mathman 2018 年 1 月 5 日
I opened the page before you edited and posted the link.
I can attach the matrix, B here but the array is too large to post. It's a 1x40 cell array with 40, 4D arrays
mathman
mathman 2018 年 1 月 5 日
What I meant by I'm not clear on how dividing the matrix would impact my final requirement was, that if I divide the 41x1 array into a cell array of n cells(D), and then apply the function would it apply the first cell of D, onto the first cell of A and so on. Because this is not what I want. I need the WHOLE array,B, applied to each 4D array within A.
Birdman
Birdman 2018 年 1 月 5 日
I see G_S variable of 41x1, then
G_S=mat2cell(G_S,41,1)
mathman
mathman 2018 年 1 月 5 日
Thanks that worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2018 年 1 月 5 日

コメント済み:

2018 年 1 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by