Problem with bsxfun and custom function

1 回表示 (過去 30 日間)
Angelo Cuzzola
Angelo Cuzzola 2020 年 5 月 25 日
回答済み: Star Strider 2020 年 5 月 25 日
I am trying to apply elementwise a custom 'division' function.
Here is an example
A = rand(4)
B = randn(5)
bsxfun(na_div,A,B)
with
function[out] = na_div(x,y);
if isnan(x) & ~isnan(y)
out = 1/y;
elseif ~isnan(x) & isnan(y)
out = x;    
else
out = x/y;    
end
I get the following error message: 'Not enough input arguments'.
The function works properly when applied to two numbers. Any idea why? Or any other solution to get the same result?
Thanks

採用された回答

Star Strider
Star Strider 2020 年 5 月 25 日
This runs without error:
A = rand(4)
B = randn(5)
bsxfun(@na_div,A,B)
function[out] = na_div(x,y);
if isnan(x) & ~isnan(y)
out = 1/y;
elseif ~isnan(x) & isnan(y)
out = x;
else
out = x/y;
end
end
except for throwing:
Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.
which is true for bsxfun generally, so not specifically with your function.
It runs without error of both matrices are the same sizes, although different arguments will likely crash it in some situations.

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by