Function to find max value from function cell array

21 ビュー (過去 30 日間)
Spyros Polychronopoulos
Spyros Polychronopoulos 2019 年 6 月 10 日
回答済み: Alex Mcaulley 2019 年 6 月 10 日
Hi there,
I have a 3 elements cell array:
A{1} = @(y) {1*y};
A{2} = @(y) {2*y};
A{3} = @(y) {3*y};
And I want to create a function that will output the maximum value of all 3 elements of A. The below is not working for y=3 or any value of y:
B = @(y) cellfun(@(x) max(x), A{1,:}(y), 'UniformOutput', false);
B(3)
any ideas?
Thanks in advance
  6 件のコメント
Adam
Adam 2019 年 6 月 10 日
Well, actually that should be fine:
B = @(y) cellfun(@(func) max(func(y)), A, 'UniformOutput', false);
should work. As y is just passed as a fixed variable to the cellfun rather than one that the cellfun itself is trying to operate over.
Spyros Polychronopoulos
Spyros Polychronopoulos 2019 年 6 月 10 日
I tried that already. Is this working for you?
A{1} = @(y) {1*y};
A{2} = @(y) {2*y};
A{3} = @(y) {3*y};
B = @(y) cellfun(@(func) max(func(y)), A, 'UniformOutput', false);
B(3)

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

採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 6 月 10 日
I see to possible answers depending on what you want:
If you want to obtain the maximum value (for the three functions) for each y:
A{1} = @(y) 1*y;
A{2} = @(y) 2*y;
A{3} = @(y) 3*y;
B = @(y) arrayfun(@(y) max(cellfun(@(func) func(y), A)),y);
y = [1,2,3,4,5]
B(y) =
ans =
3 6 9 12 15
If you want the maximum value of each separate function for all values of y the answer is the one given by @Adam:
B = @(y) (cellfun(@(func) max((func(y))), A, 'UniformOutput', false))
y = [1,2,3,4,5]
B(y) =
ans =
1×3 cell array
[5] [10] [15]

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by