Is there a way to convert function handles to nan equivalent function handles?

1 回表示 (過去 30 日間)
Udit Gupta
Udit Gupta 2015 年 9 月 2 日
コメント済み: Udit Gupta 2015 年 9 月 2 日
I am passing a cell array of function handles to my function. Typically a user might pass something like-
{@sum,@median,@mean}
Now depending upon whether or not the inputs to these functions has any NaN values I want to programmatically change it to -
{@nansum,@nanmedian,@nanmean}
Is there a way to do so?

採用された回答

Steven Lord
Steven Lord 2015 年 9 月 2 日
Yes, but it would be fragile. Would you want to detect all of these are being equivalent to plain old @sum?
@sum
@(x) sum(x)
@(x) sum(x, 1) % where x is guaranteed to be a 2-D array with nonsingleton first dimension
@(x) sum(x, 'default')
Instead, for this particular scenario I'd like to suggest a different approach. If you're using release R2015a or later, and you know that your cell array contains only some subset of the eight functions listed in the "Descriptive Statistics: Omit NaN values in basic statistical calculations" item in the Mathematics section of the release R2015a Release Notes for MATLAB, pass the 'omitnan' flag into those functions when you call them via the function handle.
  1 件のコメント
Udit Gupta
Udit Gupta 2015 年 9 月 2 日
Looks like a good solution. I did not realize there was an option to do this. Thanks!

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by