What Are Anonymous Functions?An anonymous function is a function that  dịch - What Are Anonymous Functions?An anonymous function is a function that  Việt làm thế nào để nói

What Are Anonymous Functions?An ano

What Are Anonymous Functions?
An anonymous function is a function that is not stored in a program file, but is associated with a variable whose data type is function_handle. Anonymous functions can accept inputs and return outputs, just as standard functions do. However, they can contain only a single executable statement.

For example, create a handle to an anonymous function that finds the square of a number:

sqr = @(x) x.^2;
Variable sqr is a function handle. The @ operator creates the handle, and the parentheses () immediately after the @ operator include the function input arguments. This anonymous function accepts a single input x, and implicitly returns a single output, an array the same size as x that contains the squared values.

Find the square of a particular value (5) by passing the value to the function handle, just as you would pass an input argument to a standard function.

a = sqr(5)
a =
25
Many MATLAB® functions accept function handles as inputs so that you can evaluate functions over a range of values. You can create handles either for anonymous functions or for functions in program files. The benefit of using anonymous functions is that you do not have to edit and maintain a file for a function that requires only a brief definition.

For example, find the integral of the sqr function from 0 to 1 by passing the function handle to the integral function:

q = integral(sqr,0,1);
You do not need to create a variable in the workspace to store an anonymous function. Instead, you can create a temporary function handle within an expression, such as this call to the integral function:

q = integral(@(x) x.^2,0,1);
Variables in the Expression
Function handles can store not only an expression, but also variables that the expression requires for evaluation.

For example, create a function handle to an anonymous function that requires coefficients a, b, and c.

a = 1.3;
b = .2;
c = 30;
parabola = @(x) a*x.^2 + b*x + c;
Because a, b, and c are available at the time you create parabola, the function handle includes those values. The values persist within the function handle even if you clear the variables:

clear a b c
x = 1;
y = parabola(x)
y =
31.5000
To supply different values for the coefficients, you must create a new function handle:

a = -3.9;
b = 52;
c = 0;
parabola = @(x) a*x.^2 + b*x + c;

x = 1;
y = parabola(1)
y =
48.1000
You can save function handles and their associated values in a MAT-file and load them in a subsequent MATLAB session using the save and load functions, such as

save myfile.mat parabola
Use only explicit variables when constructing anonymous functions. If an anonymous function accesses any variable or nested function that is not explicitly referenced in the argument list or body, MATLAB throws an error when you invoke the function. Implicit variables and function calls are often encountered in the functions such as eval, evalin, assignin, and load. Avoid using these functions in the body of anonymous functions.

Multiple Anonymous Functions
The expression in an anonymous function can include another anonymous function. This is useful for passing different parameters to a function that you are evaluating over a range of values. For example, you can solve the equation



for varying values of c by combining two anonymous functions:

g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1));
Here is how to derive this statement:

Write the integrand as an anonymous function,
@(x) (x.^2 + c*x + 1)
Evaluate the function from zero to one by passing the function handle to integral,
integral(@(x) (x.^2 + c*x + 1),0,1)
Supply the value for c by constructing an anonymous function for the entire equation,
g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1));
The final function allows you to solve the equation for any value of c. For example:

g(2)
ans =
2.3333
Functions with No Inputs
If your function does not require any inputs, use empty parentheses when you define and call the anonymous function. For example:

t = @() datestr(now);
d = t()
d =
26-Jan-2012 15:11:47
Omitting the parentheses in the assignment statement creates another function handle, and does not execute the function:

d = t
d =
@() datestr(now)
Functions with Multiple Inputs or Outputs
Anonymous functions require that you explicitly specify the input arguments as you would for a standard function, separating multiple inputs with commas. For example, this function accepts two inputs, x and y:

myfunction = @(x,y) (x^2 + y^2 + x*y);

x = 1;
y = 10;
z = myfunction(x,y)
z =

111

However, you do not explicitly define output arguments when you create an anonymous function. If the expression in the function returns multiple outputs, then you can request them when you call the function. Enclose multiple output variables in square brackets.

For example, the ndgrid function can return as many outputs as the number of input vectors. This anonymous function that calls ndgrid can also return multiple outputs:

c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi);
You can use the output from mygrid to create a mesh or surface plot:

z = sin(x) + cos(y);
mesh(x,y,z)


Arrays of Anonymous Functions
Although most MATLAB fundamental data types support multidimensional arrays, function handles must be scalars (single elements). However, you can store multiple function handles using a cell array or structure array. The most common approach is to use a cell array, such as

f = {@(x)x.^2;
@(y)y+10;
@(x,y)x.^2+y+10};
When you create the cell array, keep in mind that MATLAB interprets spaces as column separators. Either omit spaces from expressions, as shown in the previous code, or enclose expressions in parentheses, such as

f = {@(x) (x.^2);
@(y) (y + 10);
@(x,y) (x.^2 + y + 10)};
Access the contents of a cell using curly braces. For example, f{1} returns the first function handle. To execute the function, pass input values in parentheses after the curly braces:

x = 1;
y = 10;

f{1}(x)
f{2}(y)
f{3}(x,y)
ans =
1

ans =
20

ans =
21
0/5000
Từ: -
Sang: -
Kết quả (Việt) 1: [Sao chép]
Sao chép!
Chức năng vô danh là gì?Một chức năng ẩn danh là một chức năng mà không được lưu trữ trong một tập tin chương trình, nhưng được liên kết với một biến có kiểu dữ liệu là function_handle. Chức năng vô danh có thể chấp nhận đầu vào và trở về đầu ra, cũng giống như chức năng tiêu chuẩn làm. Tuy nhiên, họ có thể chứa chỉ là một tuyên bố thực thi duy nhất.Ví dụ: tạo một chốt để một chức năng vô danh mà tìm thấy các hình vuông của một số:sqr = @(x) x. ^ 2;Biến sqr là một xử lý chức năng. Các @ nhà điều hành tạo ra xử lý, và dấu ngoặc đơn () ngay sau khi các @ nhà điều hành bao gồm các đối số đầu vào chức năng. Chức năng ẩn danh này chấp nhận một đầu vào duy nhất x, và ngầm trở về đầu ra một đĩa đơn, một mảng cùng kích cỡ như x có chứa các giá trị bình phương.Tìm các hình vuông của một giá trị cụ thể (5) bằng cách đi qua giá trị để xử lý chức năng, cũng giống như bạn sẽ vượt qua một đối số đầu vào đến một chức năng tiêu chuẩn.một = sqr(5)a = 25Chức năng nhiều MATLAB ® chấp nhận chức năng xử lý như là đầu vào do đó bạn có thể đánh giá các chức năng trên một loạt các giá trị. Bạn có thể tạo xử lý cho chức năng vô danh hoặc nhất chức năng trong chương trình tập tin. Lợi ích của việc sử dụng chức năng vô danh là rằng bạn không cần phải chỉnh sửa và duy trì một tập tin cho một chức năng yêu cầu chỉ là một định nghĩa ngắn.Ví dụ, tìm tích phân của hàm sqr từ 0 đến 1 bằng cách đi qua các xử lý chức năng để chức năng tích phân:q = integral(sqr,0,1);Bạn không cần phải tạo ra một biến trong việc lưu trữ một chức năng vô danh. Thay vào đó, bạn có thể tạo ra một chức năng tạm thời xử lý trong một biểu hiện, chẳng hạn như cuộc gọi này để chức năng tích phân:q = integral(@(x) x. ^ 2, 0, 1);Biến trong biểu thứcChức năng xử lý có thể lưu trữ không chỉ là một biểu hiện, nhưng cũng là yếu tố biểu thức yêu cầu để đánh giá.Ví dụ, tạo ra một chức năng xử lý để một chức năng vô danh mà đòi hỏi các hệ số a, b, và c.một = 1,3;b =. 2;c = 30;parabol = @(x) một * x. ^ 2 + b * x + c;Bởi vì a, b, và c có sẵn tại thời điểm bạn tạo parabol, xử lý chức năng bao gồm các giá trị. Các giá trị vẫn tồn tại trong các chức năng xử lý ngay cả khi bạn rõ ràng các yếu tố:rõ ràng một c bx = 1;y = parabola(x)y = 31.5000Để cung cấp các giá trị khác nhau cho các hệ số, bạn phải tạo một xử lý chức năng mới:một = -3,9;b = 52;c = 0;parabol = @(x) một * x. ^ 2 + b * x + c;x = 1;y = parabola(1)y = 48.1000Bạn có thể tiết kiệm xử lý chức năng và giá trị kết hợp của họ trong một MAT tập tin và tải chúng trong một phiên họp MATLAB tiếp theo bằng cách sử dụng lưu và tải chức năng, chẳng hạn nhưlưu myfile.mat parabolSử dụng chỉ rõ ràng biến khi xây dựng chức năng vô danh. Nếu một chức năng vô danh truy cập bất kỳ biến hoặc chức năng lồng nhau không rõ ràng được tham chiếu trong danh sách đối số hoặc cơ thể, MATLAB ném một lỗi khi bạn gọi chức năng. Biến tiềm ẩn và gọi chức năng thường xuyên gặp phải trong các chức năng như eval, evalin, assignin, và tải. Tránh sử dụng các chức năng trong cơ thể của chức năng vô danh.Nhiều chức năng vô danhBiểu hiện trong một chức năng vô danh có thể bao gồm một chức năng ẩn danh. Điều này là hữu ích cho việc gửi tham số khác nhau cho một chức năng mà bạn đánh giá trên một loạt các giá trị. Ví dụ, bạn có thể giải quyết các phương trìnhcho các giá trị khác nhau của c bằng cách kết hợp hai chức năng vô danh:g = @(c) (integral(@(x) (x. ^ 2 + c * x + 1), 0, 1));Dưới đây là cách để lấy được tuyên bố này:Viết integrand là một chức năng ẩn danh,@(x) (x. ^ 2 + c * x + 1)Đánh giá các chức năng từ số không đến một bằng cách đi qua các chức năng xử lý để tách rời,Integral(@(x) (x. ^ 2 + c * x + 1), 0, 1)Cung cấp giá trị cho c bằng cách xây dựng một chức năng ẩn danh cho phương trình toàn bộ,g = @(c) (integral(@(x) (x. ^ 2 + c * x + 1), 0, 1));Cuối cùng chức năng cho phép bạn để giải quyết phương trình cho bất kỳ giá trị của c. Ví dụ:g(2)Ans = 2.3333Chức năng với đầu vào không cóNếu chức năng của bạn không yêu cầu bất kỳ đầu vào, sử dụng dấu ngoặc đơn có sản phẩm nào khi bạn xác định và gọi chức năng ẩn danh. Ví dụ:t = @() datestr(now);d = t()d =26-Jan-2012 15:11:47Omitting the parentheses in the assignment statement creates another function handle, and does not execute the function:d = td = @() datestr(now)Functions with Multiple Inputs or OutputsAnonymous functions require that you explicitly specify the input arguments as you would for a standard function, separating multiple inputs with commas. For example, this function accepts two inputs, x and y:myfunction = @(x,y) (x^2 + y^2 + x*y);x = 1;y = 10;z = myfunction(x,y)z = 111However, you do not explicitly define output arguments when you create an anonymous function. If the expression in the function returns multiple outputs, then you can request them when you call the function. Enclose multiple output variables in square brackets.For example, the ndgrid function can return as many outputs as the number of input vectors. This anonymous function that calls ndgrid can also return multiple outputs:c = 10;mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));[x,y] = mygrid(pi,2*pi);You can use the output from mygrid to create a mesh or surface plot:z = sin(x) + cos(y);mesh(x,y,z)Arrays of Anonymous FunctionsAlthough most MATLAB fundamental data types support multidimensional arrays, function handles must be scalars (single elements). However, you can store multiple function handles using a cell array or structure array. The most common approach is to use a cell array, such asf = {@(x)x.^2; @(y)y+10; @(x,y)x.^2+y+10};When you create the cell array, keep in mind that MATLAB interprets spaces as column separators. Either omit spaces from expressions, as shown in the previous code, or enclose expressions in parentheses, such asf = {@(x) (x.^2); @(y) (y + 10); @(x,y) (x.^2 + y + 10)};Access the contents of a cell using curly braces. For example, f{1} returns the first function handle. To execute the function, pass input values in parentheses after the curly braces:x = 1;y = 10;f{1}(x)f{2}(y)f{3}(x,y)ans = 1ans = 20ans = 21
đang được dịch, vui lòng đợi..
 
Các ngôn ngữ khác
Hỗ trợ công cụ dịch thuật: Albania, Amharic, Anh, Armenia, Azerbaijan, Ba Lan, Ba Tư, Bantu, Basque, Belarus, Bengal, Bosnia, Bulgaria, Bồ Đào Nha, Catalan, Cebuano, Chichewa, Corsi, Creole (Haiti), Croatia, Do Thái, Estonia, Filipino, Frisia, Gael Scotland, Galicia, George, Gujarat, Hausa, Hawaii, Hindi, Hmong, Hungary, Hy Lạp, Hà Lan, Hà Lan (Nam Phi), Hàn, Iceland, Igbo, Ireland, Java, Kannada, Kazakh, Khmer, Kinyarwanda, Klingon, Kurd, Kyrgyz, Latinh, Latvia, Litva, Luxembourg, Lào, Macedonia, Malagasy, Malayalam, Malta, Maori, Marathi, Myanmar, Mã Lai, Mông Cổ, Na Uy, Nepal, Nga, Nhật, Odia (Oriya), Pashto, Pháp, Phát hiện ngôn ngữ, Phần Lan, Punjab, Quốc tế ngữ, Rumani, Samoa, Serbia, Sesotho, Shona, Sindhi, Sinhala, Slovak, Slovenia, Somali, Sunda, Swahili, Séc, Tajik, Tamil, Tatar, Telugu, Thái, Thổ Nhĩ Kỳ, Thụy Điển, Tiếng Indonesia, Tiếng Ý, Trung, Trung (Phồn thể), Turkmen, Tây Ban Nha, Ukraina, Urdu, Uyghur, Uzbek, Việt, Xứ Wales, Yiddish, Yoruba, Zulu, Đan Mạch, Đức, Ả Rập, dịch ngôn ngữ.

Copyright ©2025 I Love Translation. All reserved.

E-mail: