How to define nested function?

조회 수: 25 (최근 30일)
Aarthika Balakumar
Aarthika Balakumar 2021년 4월 11일
편집: Cris LaPierre 2021년 4월 12일
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