matlab constant anonymous function returns only one value instead of an array

拥有回忆 提交于 2019-12-02 13:26:12

You are defining a function f = @(x,y) [0, 1]; which has the input parameters x,y and the output [0,1]. What else do you expect to happen?

Update:

This should match your description:

g=@(x,y)[zeros(size(x)),ones(size(y))]
g(x',y')

Defining an anonymous function f as

f = @(x,y) [0,1];

naturally returns [0,1] for any inputs x and y regardless of the length of those vectors.

This behavior puzzled me also until I realized that I expected f(a,b) to loop over a and b as if I had written

for inc = 1:length(a)
    f(a(inc), b(inc))
end

However, f(a,b) does not loop over the length of its inputs, so it merely returns [0,1] regardless of the length of a and b.

The desired behavior can be obtained by defining f as

g=@(x,y)[zeros(size(x)),ones(size(y))]

as Daniel stated in his answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!