Function to find max value from function cell array
古いコメントを表示
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 件のコメント
Assuming vx is an array of inputs to apply each function to something more like this, off the top of my head:
B = cellfun(@(func) max(func(vx)), A, 'UniformOutput', false);
Spyros Polychronopoulos
2019 年 6 月 10 日
Ok, but where is y coming from? Is it known at the time you call this or is it another variable input? If it is variable at the time you call the cellfun then I don't think you can use cellfun for this as you would be essentially running over two-dimensional inputs to create the answer. If it is known then just replace vx with y in the code I wrote.
Spyros Polychronopoulos
2019 年 6 月 10 日
編集済み: Spyros Polychronopoulos
2019 年 6 月 10 日
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
2019 年 6 月 10 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!