How to define nested function?

23 次查看(过去 30 天)
I want to define this f fucntion
f = @(x,y)((2.*x+y).*(x<d & y<d)+ (3.*x.*y-1).*(x>d & y<d)+...
(1+x+y).*(x<d & y>d)+ (3.*x+2.*y).*(x>d & y>d)+ ((2.*x(N/2)+y)+...
(3.*x(N/2+2).*y-1)/2).*(x==d & y<d) + (((2.*x+y(N/2))+...
(1+x+y(N/2+2)))/2).*(y==d & x<d)+(((1+x(N/2)+y)+...
(3.*x(N/2+2)+2.*y))/2).*(x==d & y>d)+(((3.*x.*y(N/2)-1)+...
(3.*x+2.*y(N/2+2)))/2).*(y==d & x>d)+(((2.*x(N/2)+y(N/2))+...
(3.*x(N/2+2).*y(N/2)-1)+(1+x(N/2)+y(N/2+2))+(3.*x(N/2+2)+...
2.*y(N/2+2)))/4).*(x==d &y==d));
If i define like this, I am getting error which is mentioned below
Error using
trying1>@(x,y)((2.*x+y).*(x<d&y<d)+(3.*x.*y-1).*(x>d&y<d)+(1+x+y).*(x<d&y>d)+(3.*x+2.*y).*(x>d&y>d)+((2.*x(N/2)+y)+(3.*x(N/2+2).*y-1)/2).*(x==d&y<d)+(((2.*x+y(N/2))+(1+x+y(N/2+2)))/2).*(y==d&x<d)+(((1+x(N/2)+y)+(3.*x(N/2+2)+2.*y))/2).*(x==d&y>d)+(((3.*x.*y(N/2)-1)+(3.*x+2.*y(N/2+2)))/2).*(y==d&x>d)+(((2.*x(N/2)+y(N/2))+(3.*x(N/2+2).*y(N/2)-1)+(1+x(N/2)+y(N/2+2))+(3.*x(N/2+2)+2.*y(N/2+2)))/4).*(x==d&y==d))
Too many input arguments.
Please help me to correct it. Thanks in advance.

回答(1 个)

Cris LaPierre
Cris LaPierre 2021-4-11
编辑:Cris LaPierre 2021-4-12
I get other errors when I try to implement your function. Perhaps you could share the code you ran that created this error?
The error message suggests you tried calling your function with more than 2 inputs.
To replicate your error, I'll define an anonymous function that has one input, but call it with 3.
a = @(x) (x-5).^2;
% correct - one input, a vector
a([3,5,7])
ans = 1×3
4 0 4
% incorrect - 3 inputs, scalars
a(3,5,7)
Error using solution
Too many input arguments.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by